运算符转换,GCC 和 clang:哪个编译器是正确的
Operator cast, GCC and clang: which compiler is right?
请考虑以下代码:
struct S {
using T = int;
operator T() { return 42; }
};
int main() {
S s;
S::T t = s;
// Is the following line correct?
t = s.operator T();
}
它使用 GCC (4.9/5.1/6.1( 编译,但无法使用 clang (3.8/3.7( 编译。
返回的错误是:
错误:未知类型名称"T";您的意思是"S::T"吗?
在这种情况下,哪个编译器是正确的,为什么?
注意
解决它是一个合格的T
问题:
t = s.operator S::T();
问题不在于如何使其工作。
我相信这是叮当错误(作为#27807提交(
来自 [basic.lookup.classref]:
如果 id 表达式是转换函数 id,则首先在 使用对象表达式和名称(如果找到(。否则,它将在整个上下文中查找 后缀表达式。在每个查找中,仅表示其专用化的类型或模板的名称 是考虑的类型。[ 示例:
struct A { }; namespace N { struct A { void g() { } template <class T> operator T(); }; } int main() { N::A a; a.operator A(); // calls N::A::operator N::A }
—结束示例 ]
在t = s.operator T();
中,T
首先在S
类中查找,它应该找到你的typedef,因此最终调用operator int()
。
相关文章:
- 奇怪的缩小转换在 g++ 编译器中加倍到浮点警告
- 在不同的编译器上转换为 void**
- 为什么我的编译器无法弄清楚这种转换,它何时存在?
- 为什么编译器抱怨 std::thread 参数在转换为右值后必须是可调用的?
- 为什么我的编译器在调用 const getter 函数时抛出"转换丢失(const)限定符"错误?
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- AIX编译器13.1.3对c++进行了不正确的双重转换
- 我把我的编译器从32位转换为64位,但我仍然不能使用超过2GB:(为什么
- 是否允许此编译器转换?
- 对从引用到 std::reference_wrapper 或编译器错误的隐式转换的理解不正确?
- std::chrono::time_point 从变量转换时编译器错误
- 将编译器命令行转换为 CMakeLists.txt用于 Casablanca Rest SDK
- C++函数到指针的隐式转换:哪个编译器是正确的?Clang和GCC不同意
- Arm GNU编译器:通过多余的强制转换优化的三进制生成的程序集
- Windows VC++编译器允许一种奇怪的c样式转换方式(或者它可能不是转换??)
- GCC 编译器对类型转换有什么作用?为什么 mac 和 Linux 上的输出不同
- 为什么C 标准要求编译器忽略对基本类型转换操作员的呼叫
- 现代编译器优化如何将递归转换为返回常数
- 编译器可以静默地从 const 转换为 const 和,反之亦然吗?
- 为什么 xlc++ 编译器抱怨强制转换右值