内部编译器错误 - 开关表达式中的模板化转换运算符 [英] Internal compiler error - Templated conversion operator in switch expression
问题描述
以下代码导致Microsoft编译器崩溃:
class Var
{
public:
template< typename T>
operator T()const
{}
};
int main()
{
var v;
switch(v)
{}
}
:代码是否正确或者编译器应该给出适当的错误?是否可以无歧义地转换为整数类型?
编译器崩溃总是一个错误, gcc
或 clang
,但两者都提供错误而不会崩溃。对于 clang
,错误是:
错误:语句需要整数类型('Var'无效)
switch(v)
^〜
< gcc
提供以下错误:
错误: 'var'
switch(v)
^
更新
$ b
添加:
operator int()const
{return 0; }
会导致与 clang
和 gcc
。
请参阅在switch语句的条件中使用模板和非模板转换运算符的类,讨论 gcc
或 clang
是正确的。我对 N3323
的解释意味着 clang在这一个上是正确的。 / p>
提交的错误报告
我一个针对ICE的错误报告,到目前为止没有反应。即使这看起来像一个奇怪的角落情况,它会导致一个内部编译器错误,应该修复。
The following code crashes the Microsoft compiler:
class Var
{
public:
template <typename T>
operator T () const
{ }
};
int main()
{
Var v;
switch (v)
{ }
}
My question: Is the code correct or should the compiler give an appropriate error? Is an unambiguous conversion to an integral type possible?
The compiler crashing is always a bug, this code does not compile on either gcc
or clang
but both provide an error without crashing. For clang
the error is:
error: statement requires expression of integer type ('Var' invalid)
switch (v)
^ ~
gcc
provides the following error:
error: ambiguous default type conversion from 'Var'
switch (v)
^
Also, note that flowing off the end of a value returning function is undefined behavior in C++.
Update
Adding:
operator int () const
{ return 0; }
to the class brings about different results from clang
and gcc
.
See Classes with both template and non-template conversion operators in the condition of switch statement for a discussion on whether gcc
or clang
is correct. My interpretation of N3323
implies clang is correct on this one.
Filed bug report
I filed a bug report for this ICE, so far no response. Even though this seems like an odd corner case it does cause an internal compiler error which should be fixed.
这篇关于内部编译器错误 - 开关表达式中的模板化转换运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!