转换运算符的模板参数类型扣除 [英] Template argument type deduction by conversion operator

查看:134
本文介绍了转换运算符的模板参数类型扣除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到C ++ 11标准的示例(n3337,14.8.2.3/7)

I see example from the C++ 11 Standard (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

并尝试通过不同的编译器重现它。我通过在转换函数中添加一个类型为T的声明,改变了一个例子。

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

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(){}

所有编译器(VS2014,gcc 5.1.0和clang 3.5.1)在t的声明中给出一个错误,这意味着T被推导为const int。这是为什么?是否有一些扩展?

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. Why is that? Is it some extension?

推荐答案

CWG问题#349 ,由开发人员打开 EDG C ++前端(显然推导出 int ,而不是 const int ):

This was covered by CWG issue #349, opened by a developer of the EDG C++ front end (which apparently deduces int, not const int):


在转换函数中执行
模板参数时,遇到了一个关于资格转换的问题。

We ran into an issue concerning qualification conversions when doing template argument deduction for conversion functions.

问题是:本例中调用的转换函数
中的T类型是什么?是Tint还是const int?

The question is: What is the type of T in the conversion functions called by this example? Is T "int" or "const int"?

如果T是int,A类中的转换函数和B失败(因为return表达式不能转换为
的函数的返回类型)。如果T是const int,则A失败,B
工作。

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). If T is "const int", A fails and B works.

因为对
转换函数的结果执行限定转换,我没有看到作为const int推导T的好处。

Because the qualification conversion is performed on the result of the conversion function, I see no benefit in deducing T as const int.

此外,我认为类A中的代码更可能发生比
代码如果类的作者计划在
返回一个指向一个const实体的指针,我会期望函数
已经写在一个const返回类型。

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.

因此,我相信正确的结果应该是T是int。

Consequently, I believe the correct result should be that T is 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;
}

我们刚刚实现了这个功能, T为int。 看来,g ++和Sun编译器将const推断​​为

We have just implemented this feature, and pending clarification by the committee, we deduce T as int. It appears that g++ and the Sun compiler deduce T as const int.

(它不存在于C ++ 03!),并且被编译器开发人员推测忽略了。

This only brought the quoted paragraph into existence (it didn't exist in C++03!), and was presumably overlooked by compiler developers.

这篇关于转换运算符的模板参数类型扣除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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