Bre*_*ier 9 c++ inheritance typedef multiple-inheritance c++11
编辑:我正在使用tdm-gcc-4.7.1-2 for Windows
不知道如何解决这个问题.我想将它用作一种类型列表,让我知道我正在尝试使用B
typedef中不存在的类型.
template <typename T, typename U>
struct A {
typedef pair<T, U> type;
};
struct B : A<int, string>, A<int, float> {};
B::type foo; // won't compile, ambiguous reference, as expected
B::A<int, int>::type bar; // compiles fine?? :(
Run Code Online (Sandbox Code Playgroud)
有没有办法让它失败A<int, int>
(以及任何其他A
没有继承的B
),或另一种方式来解决这个问题?我想我可以使用一个tuple
并通过它来is_same
递减,对每个元素进行比较与我提供的元函数进行比较,但这看起来更容易......起初:
这是因为类模板注入了模板名称; 注入的名称可以用作模板或引用模板实例化的类型(14.6.1p1).然后,派生类继承注入的类名(10.2p5); 使用它作为模板是明确的(它是相同的模板,但它是继承的)所以是允许的.
要修复您的程序,请尝试使用is_base_of
:
struct B : A<int, string>, A<int, float> { };
template<typename T, typename U>
using check_A = typename std::enable_if<std::is_base_of<A<T, U>, B>::value, A<T, U>>::type;
check_A<int, float>::type bar1; // compiles
check_A<int, int>::type bar2; // error
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
792 次 |
最近记录: |