简体   繁体   English

基于类成员的存在/缺席专门化C ++模板?

[英]Specializing C++ template based on presence/absense of a class member?

Consider the following: 考虑以下:

struct A {
  typedef int foo;
};

struct B {};

template<class T, bool has_foo = /* ??? */>
struct C {};

I want to specialize C so that C<A> gets one specialization and C<B> gets the other, based on the presence or absence of typename T::foo. 我想专门化C,以便C <A>获得一个特化,C <B>获得另一个,基于typename T :: foo的存在与否。 Is this possible using type traits or some other template magic? 这可能是使用类型特征或其他模板魔术吗?

The problem is that everything I've tried produces a compile error when instantiating C<B> because B::foo doesn't exist. 问题是我尝试的所有内容在实例化C <B>时都会产生编译错误,因为B :: foo不存在。 But that's what I want to test! 但这就是我想要测试的!


Edit: I think ildjarn's answer is better, but I finally came up with the following C++11 solution. 编辑:我认为ildjarn的答案更好,但我终于提出了以下C ++ 11解决方案。 Man is it hacky, but at least it's short. 男人是hacky,但至少它很短。 :) :)

template<class T>
constexpr typename T::foo* has_foo(T*) {
  return (typename T::foo*) 1;
}
constexpr bool has_foo(...) {
  return false;
}
template<class T, bool has_foo = (bool) has_foo((T*)0)>

Another (C++03) approach: 另一种(C ++ 03)方法:

template<typename T>
struct has_foo
{
private:
    typedef char no;
    struct yes { no m[2]; };

    static T* make();
    template<typename U>
    static yes check(U*, typename U::foo* = 0);
    static no check(...);

public:
    static bool const value = sizeof(check(make())) == sizeof(yes);
};

struct A
{
    typedef int foo;
};

struct B { };

template<typename T, bool HasFooB = has_foo<T>::value>
struct C
{
    // T has foo
};

template<typename T>
struct C<T, false>
{
    // T has no foo
};

Something like this might help: has_member . 这样的事情可能会有所帮助: has_member

typedef char (&no_tag)[1]; 
typedef char (&yes_tag)[2];

template< typename T > no_tag has_member_foo_helper(...);

template< typename T > yes_tag has_member_foo_helper(int, void (T::*)() = &T::foo);

template< typename T > struct has_member_foo {
    BOOST_STATIC_CONSTANT(bool
        , value = sizeof(has_member_foo_helper<T>(0)) == sizeof(yes_tag)
        ); }; 

template<class T, bool has_foo = has_member_foo<T>::value> 
struct C {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++-部分专门化模板类成员函数 - C++ - Partially specializing a template class member function C ++:specializing成员需要«template &lt;&gt;»语法 - C++: specializing member requires «template<>» syntax 部分专门化c ++中的模板类 - Partially specializing template class in c++ 专门用于方法C的类模板 - Class template specializing a method c++ C ++:将模板的类型参数部分专门化为另一个模板类的成员类型 - C++: Partially specializing template's type parameter as another template class's member-type 当专门化基本模板类时,C ++保持对(函数参数的)受保护成员的访问 - C++ keeping access to protected member (of the function argument) when specializing base template class c++ class 模板部分特化,未特化所有成员函数 - c++ class template partial specialization without specializing all member functions 根据 C++11 中的类型要求,专业化 class 模板成员 function - Specializing class template member function based on type requirement in C++11 C ++:“专门化”一个成员函数模板,用于处理来自某个基类的派生类 - C++: “specializing” a member function template to work for derived classes from a certain base class C ++模板部分特化 - 仅专门用于一个成员函数 - C++ template partial specialization - specializing one member function only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM