KAI*_*I42 22 c++ explicit-conversion visual-studio-2013
考虑以下程序:
#include <iostream>
class A
{
public:
A( ) { std::cout << "A()\n"; }
A( A& ) = delete;
A( int i ) { std::cout << "A( " << i << " )\n"; }
explicit operator int( ) { std::cout << "operator int()\n"; return 42; }
};
template< typename T = A > void f( T a = A() ) {}
int main( void )
{
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2013编译此代码并使用输出运行
A()
operator int()
A( 42 )
Run Code Online (Sandbox Code Playgroud)
这是编译器错误吗?看起来VS编译器在此上下文中没有注意到'explicit'关键字.根据我的理解,VS 2013错误地使用operator int()结合A(int)来排序'copy-construct'A作为f的默认参数.
两者都加了
A a;
A a1( a );
Run Code Online (Sandbox Code Playgroud)
主要和声明f为
void f( A a = A() ) {}
Run Code Online (Sandbox Code Playgroud)
不编译,VS抱怨A(A&)被删除,这似乎是正确的行为.只有在函数模板default参数的上下文中,operator int()和A(int)的组合似乎可以替代A(A&).
g ++ 4.7.3不编译代码并抱怨:
main.cpp: In function ‘int main()’:
main.cpp:21:7: error: no matching function for call to ‘A::A(A)’
main.cpp:21:7: note: candidates are:
main.cpp:10:3: note: A::A(int)
main.cpp:10:3: note: no known conversion for argument 1 from ‘A’ to ‘int’
main.cpp:6:3: note: A::A()
main.cpp:6:3: note: candidate expects 0 arguments, 1 provided
Run Code Online (Sandbox Code Playgroud)
删除'explicit'使得g ++编译代码并且输出是相同的.
这绝对是Visual C++中的一个错误.根据标准:
12.3.2转换函数[class.conv.fct]
2 - 转换函数可以是显式的(7.1.2),在这种情况下,它仅被视为在某些上下文中直接初始化(8.5)的用户定义转换(13.3.1.4,13.3.1.5,13.3.1.6) .
并且在您的示例中没有直接初始化.
其他C++编译器(如GCC和Clang)在这种情况下报告错误.
| 归档时间: |
|
| 查看次数: |
1435 次 |
| 最近记录: |