Visual Studio 2013'explicit'关键字错误? [英] Visual Studio 2013 'explicit' keyword bug?

查看:206
本文介绍了Visual Studio 2013'explicit'关键字错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

  #include< iostream> 

class A
{
public:
A(){std :: cout< 一个; }

A(A&)= delete;

A(int i){std :: cout< A(<< i<<)\\\
; }

显式运算符int(){std :: cout<< operator int()\\\
; return 42; }
};

template<类型名T = A> void f(T a = A()){}

int main(void)
{
f
return 0; Visual Studio 2013编译此代码并运行,输出为

$




$ b


$ b b
$ b

  A()
operator int()
A(42)
/ pre>

这是一个编译器错误吗?看起来像VS编译器不注意在这个上下文中的'explicit'关键字。从我的理解,VS 2013错误地使用运算符int()结合A(int)排序的'copy-construct'A作为f的默认参数。



两者添加

  A a; 
A a1(a);

到main并声明f as

  void f(A a = A()){} 

不编译,VS抱怨A(A&)被删除,这似乎是正确的行为。只有在函数模板默认参数的上下文中,运算符int()和A(int)的组合似乎可以替代A(A&)。



g ++ 4.7.3不编译代码并抱怨:

  main.cpp:在函数'int main()':
main.cpp:21:7:错误:没有匹配的函数调用'A :: A(A)'
main.cpp:21:7:note:candidate are:
main .cpp:10:3​​:note:A :: A(int)
main.cpp:10:3​​:注意:没有已知的转换从参数1从'A'到'int'
main。 cpp:6:3:注意:A :: A()
main.cpp:6:3:note:candidate期望0个参数,1提供

删除'explicit'使g ++编译代码和输出是一样的。

解决方案

这肯定是Visual C ++中的一个错误。根据标准:


12.3.2转换函数[class.conv.fct]



2 - 转换函数可以是显式的(7.1.2),在这种情况下
只被视为用户定义的直接初始化转换
(8.5)在某些上下文中.1.4,13.3.1.5,13.3.1.6)。


并且您的示例中没有直接初始化。

其他C ++编译器,如GCC和Clang在这种情况下报告错误。


Consider the following program:

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

Visual Studio 2013 compiles this code and runs, with output

A()
operator int()
A( 42 )

Is this a compiler bug? It looks like the VS compiler doesn't heed the 'explicit' keyword in this context. From my understanding, VS 2013 wrongly uses operator int() in combination with A(int) to sort-of 'copy-construct' A as the default parameter for f.

Both adding

A a;
A a1( a );

to main and declaring f as

void f( A a = A() ) {}

does not compile, VS complains that A(A&) is deleted, which seems to be correct behavior. Only in the context of the function template default parameter the combination of operator int() and A(int) seem to work as a substitution for A( A& ).

g++ 4.7.3 does not compile the code and complains:

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

Removing 'explicit' makes g++ compile the code and the output is the same.

解决方案

This is definitely a bug in Visual C++. According to standard:

12.3.2 Conversion functions [class.conv.fct]

2 - A conversion function may be explicit (7.1.2), in which case it is only considered as a user-defined conversion for direct-initialization (8.5) in certain contexts (13.3.1.4, 13.3.1.5, 13.3.1.6).

and there is no direct-initialization in your example.

Other C++ compilers such as GCC and Clang report an error in this case.

这篇关于Visual Studio 2013'explicit'关键字错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆