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

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

问题描述

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

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

尝试为构造函数2显式指定模板参数时发生编译错误。 b $ b如果我真的想显式调用构造函数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.

N.B。对于构造函数foo(),明确指定可以正常使用。

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

NB我知道它可以正常运行没有为构造函数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]:


因为显式模板参数列表遵循函数
template名称,并且由于在不使用
函数名称的情况下调用了转换成员函数模板和
构造函数成员函数模板,因此无法为这些函数模板提供显式模板
参数列表。 / p>

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天全站免登陆