c++调用模板类的特定模板构造函数

发布于 2024-11-15 19:16:31 字数 1099 浏览 1 评论 0原文

如果类也是模板,是否可以使用模板参数调用构造函数?

#include <stdio.h>
#include <iostream>

template <class A>
struct Class
{
  template <class B>
  Class(B arg) { std::cout << arg << std::endl; }
};

int main()
{
  Class<int> c<float>(1.0f);
  Class<int>* ptr = new Class<int><float>(2.0f);
  return 0;
}

编辑:所以我想调用特定模板构造函数的唯一方法是使用将参数转换为您想要的模板类型来调用它:

#include <stdio.h>
#include <iostream>

template <class A>
struct Class
{
  template <class B>
  Class(B arg) { std::cout << arg << std::endl; }

  Class(double arg) { std::cout << "double" << std::endl; }
  Class(float arg) { std::cout << "float" << std::endl; }
};

int main()
{
  Class<int> c(1.0f);
  Class<int>* ptr = new Class<int>((double)2.0f);
  return 0;
}

// 这输出: 漂浮 double

edit2:但是不属于构造函数参数本身的构造函数模板参数会发生什么情况?

template <class B, class C>
Class(B arg) { /* how do you specify ?? C */ }

Is it possible to call a constructor with template arguments if the class is a template too?

#include <stdio.h>
#include <iostream>

template <class A>
struct Class
{
  template <class B>
  Class(B arg) { std::cout << arg << std::endl; }
};

int main()
{
  Class<int> c<float>(1.0f);
  Class<int>* ptr = new Class<int><float>(2.0f);
  return 0;
}

edit: so I guess the only way to call a specific template constructor is to call it with casted paramterers to the template type you want:

#include <stdio.h>
#include <iostream>

template <class A>
struct Class
{
  template <class B>
  Class(B arg) { std::cout << arg << std::endl; }

  Class(double arg) { std::cout << "double" << std::endl; }
  Class(float arg) { std::cout << "float" << std::endl; }
};

int main()
{
  Class<int> c(1.0f);
  Class<int>* ptr = new Class<int>((double)2.0f);
  return 0;
}

// this outputs:
float
double

edit2: but what happens to constructor template arguments that are not part of the constructor arguments itself ?

template <class B, class C>
Class(B arg) { /* how do you specify ?? C */ }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

混吃等死 2024-11-22 19:16:31

在您提供的示例中,您实际上不需要显式给出template参数来调用构造函数,例如:

Class<int> c<float>(1.0f);

简单地提供参数为1.0f足够:

Class<int> c(1.0f);

同样的事情也适用于 new 示例。话虽如此,我认为您不能使用 template 参数显式调用构造函数(与普通函数不同)。

In the example you gave you actually don't need to explicitly give the template argument to invoke constructor like:

Class<int> c<float>(1.0f);

Simply providing argument as 1.0f is enough:

Class<int> c(1.0f);

Same thing is applicable for new example too. Having said that, I don't think constructor you can invoke explicitly using template argument (unlike normal function).

緦唸λ蓇 2024-11-22 19:16:31

edit2:但是不属于构造函数参数本身的构造函数模板参数会发生什么情况?

然后你可以传入一个已编码的参数

template<typename T> struct encodeType { };

struct A {
  template<typename T, typename U>
  A(T t, encodeType<U>) { }
};

A a(1, encodeType<float>());

edit2: but what happens to constructor template arguments that are not part of the constructor arguments itself ?

Then you can pass-in an argument that has it encoded

template<typename T> struct encodeType { };

struct A {
  template<typename T, typename U>
  A(T t, encodeType<U>) { }
};

A a(1, encodeType<float>());
趁微风不噪 2024-11-22 19:16:31

您需要显式声明类本身的模板类型(在您的示例中为 A)。但您无需说明 B 是什么类型。编译器从您传递 1.0f 时知道 B == float。构造函数调用中没有任何内容可以帮助编译器确定 A 是什么,因此您必须告诉它:

Class<int> c(1.0f);
Class<int>* ptr = new Class<int>(2.0f);

You need to explicitly state the template type that goes with the class itself (that's A in your example). But you don't need to say what type B is. The compiler knows from you passing 1.0f that B == float. There's nothing in the constructor call to help the compiler figure out what A is, so you have to tell it:

Class<int> c(1.0f);
Class<int>* ptr = new Class<int>(2.0f);
§普罗旺斯的薰衣草 2024-11-22 19:16:31
Class<int> c(1.0f); //f in 1.0 makes it float type!
Class<int>* ptr = new Class<int>(2.0f);

这就够了。它将调用具有模板参数float的构造函数。编译器将从参数 1.0f 推导出构造函数模板的类型参数。由于 1.0ffloat,因此编译器将推导出的类型参数是 float

同样参见这些:

Class<int> c(1.0); //this will invoke Class<int><double>(double);
Class<int>* ptr = new Class<int>(2); //this will invoke Class<int><int>(int);
Class<int> c(1.0f); //f in 1.0 makes it float type!
Class<int>* ptr = new Class<int>(2.0f);

This is enough. It will invoke the constructor which has template argument float. From the argument 1.0f, the compiler will deduce the type argument of the constructor template. Since 1.0f is float, so the type argument the compiler will deduce is float.

Likewise see these:

Class<int> c(1.0); //this will invoke Class<int><double>(double);
Class<int>* ptr = new Class<int>(2); //this will invoke Class<int><int>(int);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文