相关疑难解决方法(0)

'typedef'是否在C++类中自动继承?

我曾经认为'typedef'不会自动继承.但下面的代码表示不同的东西.

#include <iostream>
#include <type_traits>

struct A
{
    typedef int X;
};

struct A_ 
{
    typedef char X;
};

struct B : A {};
struct B_ : A, A_ {};

template< typename ... Ts >
using void_t = void;

template< typename T, typename = void >
struct has_typedef_X : std::false_type {};

template< typename T >
struct has_typedef_X< T, void_t<typename T::X> > : std::true_type {};

int main()
{
    std::cout << std::boolalpha;
    std::cout << has_typedef_X<A>::value << std::endl;
    std::cout << has_typedef_X<A_>::value << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

17
推荐指数
1
解决办法
2734
查看次数

不明确的类型引用

为什么这有效:

template <typename T> 
struct foo
{
};

struct A
{
    typedef foo<A> type;
};

struct B : public A
{
    typedef foo<B> type;
};

int main()
{
    B::type john;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但不是这个:

template <typename T> 
struct foo
{
};

template <typename T>
struct Shared
{
    typedef foo<T> type;
};

struct A : public Shared<A>
{
};

struct B : public A, public Shared<B>
{
};

int main()
{
    // g++ 4.5 says :
    // error: reference to 'type' …
Run Code Online (Sandbox Code Playgroud)

c++ templates typedef multiple-inheritance

1
推荐指数
1
解决办法
4554
查看次数

标签 统计

c++ ×2

c++11 ×1

multiple-inheritance ×1

templates ×1

typedef ×1