gpt4 book ai didi

C++ 模板 : how to determine if a type is suitable for subclassing

转载 作者:可可西里 更新时间:2023-11-01 15:10:59 26 4
gpt4 key购买 nike

假设我有一些模板类取决于类型T . T几乎可以是任何东西:int , int* , pair <int, int>struct lol ;它不能是void ,引用或任何 cv 合格的东西。对于一些优化,我需要知道我是否可以子类化 T .所以,我需要一些特征类型 is_subclassable ,确定为基本特征的逻辑组合或通过一些 SFINAE 技巧。

在原来的例子中,intint*不可子类化,而 pair <int, int>struct lol是。

编辑:正如 litb 在下面指出的, union 也不可子类化并且 T也可以是 union 类型。

如何编写我需要的特征类型?

最佳答案

你要判断是否是非 union 类。我不知道有什么办法可以做到这一点(而且 boost 也没有找到办法)。如果您可以忍受 union 案例误报,则可以使用 is_class

template<typename> struct void_ { typedef void type; };

template<typename T, typename = void>
struct is_class { static bool const value = false; };

template<typename T>
struct is_class<T, typename void_<int T::*>::type> {
static bool const value = true;
};

Boost 有一个 is_union虽然它使用特定于编译器的内置函数,但这将在此处为您提供帮助。 is_class (boost 也提供)与 is_union 结合将解决您的问题。

关于C++ 模板 : how to determine if a type is suitable for subclassing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6540948/

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