我尝试在没有运气的情况下使用谷歌搜索,所以我在这里尝试。
我有几个类,每个类都定义一个 member struct foo。此成员类型foo本身可以从以前的类之一继承,因此foo自己获得成员类型。
我想foo使用模板元编程访问嵌套类型(见下文),但是 c++ 名称注入会带来问题,因为上层foo类型名称被注入到下层foo类型中,当我想访问下层类型时,上层类型会被解析,比如使用A::foo::foo.
这是一个例子:
#include <type_traits>
struct A;
struct B;
struct A {
struct foo;
};
struct B {
struct foo;
};
struct A::foo : B { };
struct B::foo : A { };
// handy c++11 shorthand
template<class T>
using foo = typename T::foo;
static_assert( std::is_same< foo< foo< A > >, foo< B > >::value,
"this should not fail (but it does)" );
static_assert( std::is_same< foo< foo< A > >, foo< A > >::value,
"this should fail (but it does not)" );
仅供参考,我正在实现函数导数,foo是导数类型。上述情况发生在例如 sin/cos 中。
TLDR:我怎样才能foo<foo<A>>成为foo<B>,不是foo<A>?
谢谢 !