我试图提供用于检查的结构A
是(选择演员)-可演员B
。
所有四个转换都将具有完全相同的实现,除了它们的名称(本地宏定义是可能的,但不是必需的)。我写了很多检查运算符结构,例如:
#include <iostream>
#include <type_traits>
#include <string>
template<class, class, class, class = void>
struct is_valid_ternary_operator : std::false_type
{ };
template<class T, class S, class R>
struct is_valid_ternary_operator <T, S, R,
std::void_t<decltype(std::declval<T>() ?
std::declval<S>() :
std::declval<R>())>> : std::true_type
{ };
int main()
{
//true? 1 : 0 //is ok
std::cout << is_valid_ternary_operator<bool, int, int>::value << std::endl;
//true? 1 : std::string("0") //would be error
std::cout << is_valid_ternary_operator<bool, int, std::string>::value << std::endl;
//true? std::string("1") : std::string("0") //ok
std::cout << is_valid_ternary_operator<bool, std::string, std::string>::value << std::endl;
//std::string("1")? 1 : 0 //error
std::cout << is_valid_ternary_operator<std::string, int, int>::value << std::endl;
}
实例 http://coliru.stacked-crooked.com/a/f3bceabfe478caaf
显示预期输出。但现在考虑对强制类型转换执行相同的操作:
template<class T, class S, class = void>
struct is_static_cast_able : std::false_type
{ };
template<class T, class S>
struct is_static_cast_able<T, S,
std::void_t<decltype(static_cast<std::declval<S>()>
(std::declval<T>()))>> : std::true_type
{ };
但它会产生错误:
main.cpp:12:84: error: template argument 1 is invalid
(std::declval<T>()))>> : std::true_type
^~
main.cpp:12:94: error: template argument 3 is invalid
(std::declval<T>()))>> : std::true_type
^~~~~~~~~
Live http://coliru.stacked-crooked.com/a/cb4401c17fbb1812
是否不允许在未评估的上下文中使用强制转换?