简体   繁体   English

具有模板构造函数(其他类型)的模板类

[英]templated class with templated constructor (of other type)

Is it possible to have a templated class and also templating the constructor with some other type? 是否可以有一个模板化的类,并以其他类型来模板化构造函数?

something like this: 像这样的东西:

template<typename T1>
class Foo{
    template<typename T2>
    Foo(T1 aBar, T2 dummyArgument){
        bar = aBar;
        bytesOfT2 = sizeof(T2);
    };

    int bytesOfT2;
    T1 bar;
};

is this possible? 这可能吗? and if so, how would I call such a constructor? 如果是这样,我怎么称呼这样的构造函数? Do I need to consider something in regards of header and cpp files? 我需要考虑一些有关头文件和cpp文件的问题吗?

thanks! 谢谢!

//edit: my particular example is actually even a little bit more complicated. // edit:我的特定示例实际上要复杂得多。 i have 我有

template <typename U1, U2>
class Foo{
    U1 var1;
    U2 var2;
};

template <typename T1>
class Bar{
    template<typename T2, typename T3>
    Bar(Foo<T2,T3> aFoo, T1 aVal){
        val=aVal;
        bytesOfT2=sizeof(T2);
        bytesOfT3=sizeOf(T3);
    };

int bytesOfT2;
int bytesOfT3;
T1 val;
};

does it mean i can here call the constructor just with any variable of type Foo and it should automatically select the proper Constructor acording to the particular version of Foo (for example if the variable i pass is of type Foo should it automatically set T2 to bool and T3 to float)? 这是否意味着我可以在这里仅使用Foo类型的任何变量来调用构造函数,并且它应根据Foo的特定版本自动选择适当的构造函数(例如,如果我传递的变量为Foo类型,则它应将T2自动设置为bool和T3浮动)?

Yes, a class template can have a constructor template. 是的,一个类模板可以具有一个构造函数模板。 You call it as you would call any other constructor: 您可以像调用其他任何构造函数一样调用它:

Foo<int> my_foo(42, 0.0);

This invokes the constructor template with T1 = int (because T1 is a class template parameter and the class template argument is int ) and T2 = double (because T2 is a function template argument and is deduced from the argument 0.0 ). 这将调用T1 = int (因为T1是类模板参数,而类模板参数是int )和T2 = double (因为T2是函数模板参数并从参数0.0推导)来构造函数模板。

All of the template arguments have to be able to be deduced from the function arguments, otherwise the constructor template cannot be called. 必须能够从函数参数中推导出所有模板参数,否则无法调用构造函数模板。 There is no way to explicitly specify the template arguments for a constructor template. 无法为构造函数模板明确指定模板参数。

Yes, it's possible. 是的,有可能。 As for calling the constructor, you just supply one argument of type T1 and a second argument of type T2 . 至于调用构造函数,您只需提供一个T1类型的参数和另一个T2类型的参数。 That's it (unless one gets into a terminology discussion about "calling"). 就是这样(除非有人进入有关“呼叫”的术语讨论)。

By the way, instead of first default-initializing bar and then assigning to it, you should just initialize it directly, like 顺便说一句,而不是第一个默认初始化bar ,然后分配给它,你应该直接初始化它,像

template< class T2 >
Foo( T1 const& aBar, T2 const& dummyArgument )
    : bar( aBar )
    , bytesOfT2( sizeof( T2 ) )
{}

Look up constructors and initializers in your C++ textbook. 在C ++教科书中查找构造函数和初始化程序。

Cheers & hth., 干杯,……

You can also use more than one template type : 您还可以使用多种template type

template<typename T1, typename T2>
class Foo{
public:
    Foo(T1 aBar, T2 dummyArgument){
        bar = aBar;
        bytesOfT2 = sizeof(T2);
    }

private:
    int bytesOfT2;
    T1 bar;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM