[英]Template argument type deduction by conversion operator
I see example from the C++ 11 Standard (n3337, 14.8.2.3/7) 我看到C ++ 11标准中的例子(n3337,14.8.2.3/7)
struct A {
template <class T> operator T***();
};
A a;
const int * const * const * p1 = a; // T is deduced as int, not const int
and try to reproduce it by different compilers. 并尝试由不同的编译器重现它。 I changed the example a little by adding a declaration with type T in a conversion function
我通过在转换函数中添加类型为T的声明来稍微改变了示例
struct A {
template <class T> operator T***()
{
T t; //if T==const int, then it is error (uninitialized const)
return nullptr;
}
};
A a;
const int * const * const * p1 = a;
int main(){}
All the compilers (VS2014, gcc 5.1.0 and clang 3.5.1) give an error in the declaration of "t", which means T is deduced as const int. 所有编译器(VS2014,gcc 5.1.0和clang 3.5.1)都在“t”的声明中给出了一个错误,这意味着T被推导为const int。 Why is that?
这是为什么? Is it some extension?
这是一些扩展吗?
This was covered by CWG issue #349 , opened by a developer of the EDG C++ front end (which apparently deduces int
, not const int
): 这由CWG问题#349涵盖,由EDG C ++前端的开发人员打开(显然推断出
int
,而不是const int
):
We ran into an issue concerning qualification conversions when doing template argument deduction for conversion functions.
在为转换函数执行模板参数推导时,我们遇到了有关资格转换的问题。
The question is: What is the type of T in the conversion functions called by this example?
问题是:此示例调用的转换函数中的T类型是什么? Is T "int" or "const int"?
是T“int”还是“const int”?
If T is "int", the conversion function in class A works and the one in class B fails (because the return expression cannot be converted to the return type of the function).
如果T是“int”,则类A中的转换函数起作用,而类B中的转换函数失败(因为返回表达式无法转换为函数的返回类型)。 If T is "const int", A fails and B works.
如果T是“const int”,则A失败并且B工作。
Because the qualification conversion is performed on the result of the conversion function, I see no benefit in deducing T as const int.
因为对转换函数的结果执行了限定转换,所以我认为将T推导为const int没有任何好处。
In addition, I think the code in class A is more likely to occur than the code in class B. If the author of the class was planning on returning a pointer to a const entity, I would expect the function to have been written with a const in the return type.
另外,我认为A类中的代码比B类中的代码更容易出现。如果类的作者计划返回指向const实体的指针,我希望该函数是用返回类型中的const。
Consequently, I believe the correct result should be that T is int.
因此,我认为正确的结果应该是T是int。
struct A { template <class T> operator T***() { int*** p = 0; return p; } }; struct B { template <class T> operator T***() { const int*** p = 0; return p; } }; int main() { A a; const int * const * const * p1 = a; B b; const int * const * const * p2 = b; }
We have just implemented this feature, and pending clarification by the committee, we deduce T as int.
我们刚刚实施了这个功能,并且在委员会要求澄清之前,我们将T推断为int。 It appears that g++ and the Sun compiler deduce T as const int.
似乎g ++和Sun编译器将T推导为const int。
This only brought the quoted paragraph into existence (it didn't exist in C++03!), and was presumably overlooked by compiler developers. 这只引入了引用的段落(它在C ++ 03中不存在!),并且可能被编译器开发人员忽略了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.