c++ - static/dynamic/const/reinterpret_cast 可以在未评估的上下文中使用吗?

标签 c++ casting c++14 c++17

我试图提供结构来检查 A 是(选择强制转换)-castable 到 B。 所有四个强制类型转换都将具有完全相同的实现,期望它们的名称(本地宏定义是可能的,但不是必需的)。我写了很多 check-for operators 结构,例如:

#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;
}

Live example

显示预期的输出。但现在考虑对类型转换做同样的事情:

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

是否不允许在未评估的上下文中使用强制转换?

最佳答案

Is using casts in unevaluated context not allowed?

简短回答:是的,这是允许的。


无论如何,我会尝试将示例简化为一个最小的、有效的示例。
请注意,转换表达式,它们应该与操作数未计算的运算符一起使用(sizeofnoexcept, decltype, typeid), 除非明确说明相反。

例如,sizeof 是一个未计算的上下文:

int main() {
    unsigned int i;
    sizeof(static_cast<int>(i));
}

一个简单得多的例子,而且它有效。
同样可以使用 decltype 显示,其操作数也未计算:

int main() {
    unsigned int i;
    decltype(static_cast<int>(i)) j = i;
}

等等,我们可以用noexcept做类似的事情:

int main() {
    unsigned int i;
    bool b = noexcept(static_cast<int>(i));
}

还有 typeid:

#include <typeinfo>

int main() {
    unsigned int i;
    auto b = typeid(static_cast<int>(i)).name();
}

关于c++ - static/dynamic/const/reinterpret_cast 可以在未评估的上下文中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669783/

相关文章:

java - 无法从 View 转换到 EditText

转换数组和结构体

c++ - C++中结构的输入元素

c++ - 自记录类型别名(typedef)以指示它将在另一个特定类中使用

c++ - 将世界、 View 和投影矩阵传递给 Direct3D 中的着色器

c++ - STL 的发现背后的算法是什么?

c++ - 显示在 C++ 中创建临时文件的位置

c++ - [[除了属性外还有什么 'alternate grammar'?

c++ - 使用多重继承进行转换

c++ - 使用 boost spirit x3 进行语义检查