gpt4 book ai didi

c++ - 用于派生特定类型的函数模板特化

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:37 25 4
gpt4 key购买 nike

class base
{
};

class derived
{
};

template<class T> void foo() {}


int main()
{
foo<int>();
foo<derived>();
}

我想将 foo 专门化为 T = base 的推导。这是可能的还是我需要专门针对 base 本身?

最佳答案

您可以组合 boost::enable_if 和 boost::is_base_of,如文档所述in the manual for boost::enable_if .

template <class T>
T foo(typename enable_if<boost::is_base_of<base,T> >::type* dummy = 0);

关于c++ - 用于派生特定类型的函数模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7823727/

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