gpt4 book ai didi

c++ - 可以显式指定构造函数的模板参数吗?

转载 作者:IT老高 更新时间:2023-10-28 12:43:01 29 4
gpt4 key购买 nike

类的构造函数可以是模板函数。在调用此类构造函数时,编译器通常会查看提供给构造函数的参数并从中确定使用的模板参数。是否还有一些语法可以显式指定模板参数?

一个人为的例子:

struct A {
template<typename T>
A() {}
};

有没有办法实例化这个类?显式指定构造函数的模板参数的语法是什么?

如果编译器似乎没有找到正确的模板化构造函数,我的用例将是一个问题。显式指定模板参数可能会生成更有用的错误消息,甚至可以解决问题。

最佳答案

没有。 C++03 标准说:

[Note: 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.] (§14.5.2/5)

关于c++ - 可以显式指定构造函数的模板参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861839/

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