检查类是否在继承层次结构中明确定义成员类型

时间:2018-01-07 11:31:26

标签: c++ c++11 templates c++14 sfinae

我有一组使用成员typedef Next链接的类,如下所示:

class Y; class Z;
class X { public: typedef Y Next; };
class Y { public: typedef Z Next; };
class Z { };

我需要一种从链的任何类开始获取链的最终类的方法。感谢accepted answer of this post,我写了以下代码:

// cond_type<Condition, Then, Else>::type   // selects type 'Then' if 'Condition' is true, or type 'Else' otherwise
template <bool Condition, typename Then, typename Else = void>
struct cond_type
{
    typedef Then type;
};
template <typename Then, typename Else>
struct cond_type<false, Then, Else >
{
    typedef Else type;
};

template <class C, typename _ = void> 
struct chain
{
    typedef C last; 
};
template <class C>
struct chain<C, typename cond_type<false, typename C::Next>::type>
{
    typedef typename chain<typename C::Next>::last last;
};

使用上面的模板chain<C>::last,以下代码正确地实例化了类Z的3个对象,如预期的那样:

chain<X>::last z1;
chain<Y>::last z2;
chain<Z>::last z3;

但是,如果所考虑的类集形成继承层次结构,则采用以下方式:

class U; class V;
class T             { public: typedef U Next; };
class U : public T  { public: typedef V Next; };
class V : public U  { };

然后,使用模板chain<C>::last,以及上述集合中的任何类C,例如:

chain<T>::last v;

导致以下编译错误:

1>test.cpp(88): error C3646: 'last' : unknown override specifier

我理解问题是类V继承自父类typedef V Next中定义的U,导致编译模板chain<V,V>的特殊形式应该使用通用的,因为V没有成员Next

无论如何,我被困在这里,因为我需要一种有效的机制,即使在这种类层次结构的情况下也是如此。

我怎么能这样做?

PS:类之间的继承必须保持公开;成员typedef必须保持公开。

2 个答案:

答案 0 :(得分:6)

这很简单:

template <typename T, typename = void> struct last_t_impl
{
    using type = T;
};
template <typename T> struct last_t_impl
    <T, std::enable_if_t<!std::is_same_v<typename T::Next, T>>>
{
    using type = typename last_t_impl<typename T::Next>::type;
};
template <typename T> using last_t = typename last_t_impl<T>::type;

用法:

last_t<T> v1;
last_t<U> v2;
last_t<V> v3;

如果您需要上面的代码来编译C ++ 14(而不是C ++ 17),请将std::is_same_v<A,B>更改为std::is_same<A,B>::value

请注意,您的typename cond_type<false, T>::type可以替换为std::void_t<T>(或C ++ 14中的std::conditional_t<false,T,void>)。但在这种情况下,它不是必需的,因为链的末端将由std::is_same_v<typename T::Next, T>检测到SFINAE。 (即使T::Next由于某种原因不存在,SFINAE仍将启动,last_t<T>将只是T。)

答案 1 :(得分:0)

类型链接的类应该作为类层次结构的单独抽象实现。

#include <iostream>
#include <string>
#include <type_traits>

// Definitions for TypeChain as an abstraction

// Simple interface to declare next class for a class.
template <typename N>
struct Next {
    using next = N;
};

// Primary template for a type chain.
template <typename T>
struct TypeChain : Next<void>{};


/// Implementation of type-function last.
template <typename T, typename C = typename TypeChain<T>::next> struct last_t_impl
{
    using type = std::conditional_t<std::is_same_v<C, void>, T, typename last_t_impl<C>::type>;
};

// Never used, but needed to end recursion.
template <> struct last_t_impl<void, void>
{
    using type = void;
};

template <typename T> using last_t = typename last_t_impl<T>::type;

// Définition of the class hierarchy, without chaining.
class T             { };
class U : public T  { };
class V : public U  { };

// Definition of the chain.
// T => U => V

// Specialisation of TypeChain for T
template<>
struct TypeChain<T> : Next<U> {};

// Specialisation of TypeChain for U
template<> 
struct TypeChain<U> : Next<V>{};

// No specialisation for V, since it has no next value.


// Test (should run three 1)
int main()
{
    std::cout << std::is_same_v<last_t<T>,  V> << "\n";
    std::cout << std::is_same_v<last_t<U>,  V> << "\n";
    std::cout << std::is_same_v<last_t<V>,  V> << "\n";
}

它无法检测到链循环,但是这个实现将两个抽象(类层次结构和类型链)保持为单独的概念,因此您可以提供这些功能,而无需修改您的类层次结构,这更好,在我的图。

相关问题