gpt4 book ai didi

c++ - 模板构造函数怪异

转载 作者:行者123 更新时间:2023-11-30 03:06:44 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Can the template parameters of a constructor be explicitly specified?

跟进我之前的 question , (我在edit 2中发现了这种情况)

代码简单:

#include <iostream>

struct Printer
{
Printer() { std::cout << "secret code" << std::endl; }
};

template <class A>
struct Class
{
template <class B, class C>
Class(B arg)
{
C c; /* the 'secret code' should come from here */
std::cout << arg << std::endl;
}

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

/* this forbids the use of printer in the first parameter */
Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};

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

// Can anyone print 'secret code' while creating an object of type 'Class' ?

详解:对于模板构造函数,是否可以在实例化对象时指定一个不属于构造函数参数的模板参数?

我认为这值得单独提出一个问题。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com