本文介绍了为什么C ++要求我在初始化列表中重复基类的模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在将一些代码从MSVC(没有permissive-)移植到linux,并且我了解到,如果您在类的初始值设定项列表中调用模板基类的构造函数,则必须指定所有模板参数,否则会出现错误.似乎有点多余,因为如果您在重新输入模板参数时犯了一个错误,那将是一个硬错误:

I was porting some code from MSVC(without permissive-) to linux and I learned that if you call constructor of a template base class in initializer list of your class you must specify all the template parameters or you get an error.Seems kind of redundant, since if you make a mistake in retyping the template parameters it is a hard error:

完整代码在这里:

template <typename T, bool has_x>
struct Base
{
    Base(T t): t_(t){
    }
    T t_=0;
};



template <typename T>
class Derived : public Base<T, false>
{
public:
    // : Base<T, true> is hard error
    Derived(const T& t) : Base<T, false>(t) {}
};

int main()
{
    Derived d(47);
}

是否有很强的理由,或者只是标准化过程从来没有花时间处理这种用例?

Is there a strong reason for this, or just standardization process never took time to special case this use case?

推荐答案

仅当Derived是模板且基础类型取决于其模板参数时,才需要这样做.

You only need to do that when Derived is a template and the type of the base depends on its template parameters.

例如,编译如下:

template <typename T>
class Derived : public Base<int, false>
{
public:
    Derived(const T& t) : Base(t) {}
};

据我所知,这里(在成员初始值设定项列表中)Base实际上是Base<...>的注入类名称,它像其他所有内容一样从其继承.

As far as I know, here (in member initializer list) Base is actually the injected-class-name of Base<...>, inherited from it like everything else.

如果基础的类型确实取决于模板参数,则其继承的注入类名称将变得不可访问(至少直接访问),就像从其继承的任何其他成员一样.

And if the type of the base does depend on the template parameters, its inherited injected-class-name becomes inaccessible (at least directly), just like any other member inherited from it.

对于成员变量/函数,您需要添加this->来访问它,但是对于类型成员,则需要Derived:::

For a member variable/function, you'd add this-> to access it, but for a type member you need Derived:::

template <typename T>
class Derived : public Base<T, false>
{
public:
    Derived(const T& t) : Derived::Base(t) {}
};

这篇关于为什么C ++要求我在初始化列表中重复基类的模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:47