gpt4 book ai didi

C++ 模板化构造函数 : Do they prevent certain types from being inferred?

转载 作者:太空狗 更新时间:2023-10-29 21:24:35 24 4
gpt4 key购买 nike

如果我有这样的代码

struct Foo
{
template<class T>
Foo(T arg) { }
};

这是否会阻止 T 成为constvolatile 或引用?

同样,如果我有

struct Bar
{
template<class T>
Bar(T const volatile &arg) { }
};

这是否意味着 T 永远不会是 constvolatile 或引用?

从本质上讲,这是否意味着构造函数模板参数必须被推断,即不能指定为推断值以外的任何值?

最佳答案

是的,您不能在调用构造函数模板时显式指定模板参数。

因为您从不直接调用它,而总是通过强制转换或声明隐式调用它。

您只能在显式特化或实例化时指定它们。这不是很有用,但可能

// explicit instantiation
template Bar::Bar<const int>(const int);

关于C++ 模板化构造函数 : Do they prevent certain types from being inferred?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732356/

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