gpt4 book ai didi

c++ - 模板类不同的返回类型取决于类参数

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

我有这样的类(class):

template<class T>
class A{
// lots of methods here

Something get();
};

如果 T 是特定类,我希望 Something get() 变成 const Something &get()

首先我尝试通过继承来实现它,但它变得太困惑了。

我可以这样做吗,而不专门化整个 class A 并且不引入第二个模板。

最佳答案

您只需用一种简单易用的类英语语言将其写下来即可。

std::conditional<std::is_same<T, SpecificClass>::value, 
Something const&,
Something>::type get();

关于c++ - 模板类不同的返回类型取决于类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022733/

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