从模板基类的模板构造函数派生 [英] Derive from template constructor of template base class

查看:118
本文介绍了从模板基类的模板构造函数派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,是否有可能从模板类继承,并在派生类的构造函数中调用基类的构造函数,该基类也是模板化的,并且没有参数可推导其类型?

Just curious, is it ever possible to inherit from a template class and in constructor of the derived class, call constructor of the base class which is also templated and has no arguments to deduce its types from?

template<typename T>
struct Base {
    template<typename D>
    Base() {                // no argument of type D to infer from
        static_assert(std::is_same<T,D>::value, "");
    }
};

struct Derived : Base<int> {
    Derived()  : Base<int>::Base<int>() {} // is there a way to write it correctly?
};

在我的特殊情况下,我可以用模板方法替换模板构造函数,但这仍然是一个有趣的问题

I can replace template constructor by a template method in my particular case, but still it is an interesting question about the language flexibility.

推荐答案

C ++标准对此有何评论(第14.8.1节):

What the C++ standard says about this (section 14.8.1):


[注:因为显式模板参数列表在函数模板名称之后,并且由于在不使用函数名称的情况下调用转换成员函数模板和构造函数成员函数模板,无法为这些功能模板提供明确的模板参数列表。 —尾注]

[ Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. — end note ]

这是一条注释,而不是规则,因为它实际上是另外两个规则(同一个规则)的结果部分:

It's a note, not a rule, because it actually is a consequence of two other rules, one in the same section:


在引用函数模板专门化时,可以通过使用模板参数列表中的函数模板名称限定来指定模板参数

Template arguments can be specified when referring to a function template specialization by qualifying the function template name with the list of template-arguments in the same way as template-arguments are specified in uses of a class template specialization.

并从12.1


构造函数没有名称。

Constructors do not have names.

这篇关于从模板基类的模板构造函数派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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