类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数? [英] Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?

查看:23
本文介绍了类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?

Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?

尝试为构造函数 2 显式指定模板参数时出现编译错误.如果我真的想显式调用构造函数 2 应该怎么做?

compile error when tried to explicit specify template argument for constructor 2. How should I do it if I really want to explicit call constructor 2 ?

请注意,当您要明确指定删除器类型时,这与 boost::shared_ptr 的情况相同.

Please note this is the same situation for boost::shared_ptr when you want to explicitly specify the deleter type.

注意对于-构造函数foo(),明确指定工作正常.

N.B. For non-construction function foo(), explicitly specify works fine.

N.B 我知道它工作正常没有为构造函数 2 明确指定第二个作为模板参数推导通常工作正常,我只是好奇如何明确指定它.

N.B I know it works fine without specify the 2nd one explicitly for the constructor 2 as template argument deduction normally just works fine, I am just curious how to specify it explicitly.

template<class T> class TestTemplate {
public:
    //constructor 1
    template<class Y> TestTemplate(T * p) {
        cout << "c1" << endl;
    }

    //constructor 2
    template<class Y, class D> TestTemplate(Y * p, D d) {
        cout << "c2" << endl;
    }

    template<class T, class B>
    void foo(T a, B b) {
        cout << "foo" << endl;
    }
};

int main() {
    TestTemplate<int> tp(new int());//this one works ok call constructor 1
    //explicit template argument works ok
    tp.foo<int*, string>(new int(), "hello");

    TestTemplate<int> tp2(new int(),2);//this one works ok call constructor 2

    //compile error when tried to explicit specify template argument for constructor 2
    //How should I do it if I really want to explicit call constructor 2?
    //TestTemplate<int*, int> tp3(new int(), 2); //wrong
    //TestTemplate<int*> tp3<int*,int>(new int(), 2); //wrong again

    return 0;
}

推荐答案

修复您的代码,以下内容将起作用:

Fixing your code, the following would work:

template<class T> class TestTemplate {
public:
    //constructor 1
    template<class Y> TestTemplate(Y * p) {
        cout << "c1" << endl;
    }

    //constructor 2
    template<class Y, class D> TestTemplate(Y * p, D d) {
        cout << "c2" << endl;
    }

    template<class A, class B>
    void foo(A a, B b) {
        cout << "foo" << endl;
    }
};

int main() {
    TestTemplate<int> tp(new int());

    tp.foo<int*, string>(new int(), "hello");

    TestTemplate<int> tp2(new int(),2);
}

您不能将 T 用于类模板参数 构造函数模板参数.但是,要回答您的问题,来自 [14.5.2p5]:

You cannot use T for the class template parameter and the constructor template parameter. But, to answer your question, from [14.5.2p5]:

因为显式模板参数列表跟在函数后面模板名称,因为转换成员函数模板和调用构造函数成员函数模板时不使用函数名,无法提供显式模板这些函数模板的参数列表.

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.

因此,您不能为构造函数显式指定模板参数.

Therefore, you cannot explicitly specify template arguments for constructor.

这篇关于类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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