C++ 是否可以在派生类中选择性地定义类型

C++ 是否可以在派生类中选择性地定义类型,c++,templates,C++,Templates,我有一个类模板,如下所示: template <Base> struct foo : Base { typedef int some_type; }; struct some_base { typedef float some_type; }; template <typename T> struct has_some_type { typedef char no; // type with sizeof == 1

我有一个类模板,如下所示:

template <Base>
struct foo : Base
{
  typedef int some_type;
};
struct some_base
{
  typedef float some_type;
};
template <typename T>
struct has_some_type
{
    typedef char no;                    // type with sizeof == 1
    typedef struct { char x[2]; } yes;  // type with sizeof == 2

    template <typename X, typename Y = typename X::some_type>
    struct foo {};

    template <typename X>
    static yes test(foo<X>*);

    template <typename X>
    static no test(...);

    static const bool value = (sizeof(test<T>(0)) == sizeof(yes));
};
现在
foo::some_type
将成为
int
,因为派生的
foo
将隐藏
Base::some_type
。我想做的是,如果定义了
Base::some_type
,那么使用其他方法,将
some_type
foo
中本地定义为“
int
”——所以问题是,这可能吗


我可以颠倒这种关系,省去一些麻烦,但是在实际应用程序中它不是很符合逻辑…

struct foo
一个默认为
int
的额外模板参数:

template <Base, Typedef = int>
struct foo : Base
{
  typedef Typedef some_type;
};
模板
结构foo:Base
{
类型定义类型定义某些类型;
};

然后
foo::some_type
就是
some_base::some_type

只要有一点模板元编程,任何事情都是可能的:)

首先编写一个元函数,确定一个类型是否具有名为“some_type”的嵌套类型。大概是这样的:

template <Base>
struct foo : Base
{
  typedef int some_type;
};
struct some_base
{
  typedef float some_type;
};
template <typename T>
struct has_some_type
{
    typedef char no;                    // type with sizeof == 1
    typedef struct { char x[2]; } yes;  // type with sizeof == 2

    template <typename X, typename Y = typename X::some_type>
    struct foo {};

    template <typename X>
    static yes test(foo<X>*);

    template <typename X>
    static no test(...);

    static const bool value = (sizeof(test<T>(0)) == sizeof(yes));
};
模板
结构具有某种类型
{
typedef char no;//类型的sizeof==1
typedef struct{char x[2];}yes;//类型的sizeof==2
模板
结构foo{};
模板
静态是测试(foo*);
模板
静态无试验(…);
静态常量布尔值=(sizeof(test(0))==sizeof(yes));
};
现在,您可以在派生类中执行以下操作:

template <typename T, bool has_some_type>
struct get_some_type;

template <typename T>
struct get_some_type<T, true>
{
    typedef typename T::some_type type;
};

template <typename T>
struct get_some_type<T, false>
{
    typedef int type;  // the default type
};

template <typename base>
class derived : base
{
    typedef typename get_some_type<base, has_some_type<base>::value>::type some_type;

    ...
};
模板
结构获取某种类型;
模板
结构获取一些类型
{
typedef typename T::some_type类型;
};
模板
结构获取一些类型
{
typedef int type;//默认类型
};
模板
派生类:基
{
typedef typename get_some_type::type some_type;
...
};
这应该可以:

struct sfinae_types
{
  struct yes { char x; };
  struct no  { char x[2]; };
};

template<class T>
class has_some_type : sfinae_types
{
  private:
    template<class U>
    static yes test(typename U::some_type *);
    template<class U>
    static no test(...);
  public:
    enum { value = (sizeof(yes) == sizeof(test<T>(0))) };
};

template<bool, class T, typename DT>
struct get_some_type
{
  typedef DT type;
};

template<class T, typename DT>
struct get_some_type<true, T, DT>
{
  typedef typename T::some_type type;
};

struct B1
{
};

struct B2
{
  typedef float some_type;
};

template<typename T>
struct D : T
{
  typedef typename get_some_type<has_some_type<T>::value, T, int>::type some_type;
};

#include<iostream>
#include<typeinfo>

int main()
{
  std::cout << has_some_type<B1>::value << std::endl;
  std::cout << typeid(D<B1>::some_type).name() << std::endl;
  std::cout << has_some_type<B2>::value << std::endl;
  std::cout << typeid(D<B2>::some_type).name() << std::endl;
  return(0);
}
struct sfinae\u类型
{
结构yes{char x;};
结构号{char x[2];};
};
模板
类有一些类型:sfinae类型
{
私人:
模板
静态yes测试(typename U::some_type*);
模板
静态无试验(…);
公众:
枚举{value=(sizeof(yes)==sizeof(test(0)))};
};
模板
结构获取一些类型
{
DT型;
};
模板
结构获取一些类型
{
typedef typename T::some_type类型;
};
结构B1
{
};
结构B2
{
typedef浮动某些_类型;
};
模板
结构D:T
{
typedef typename get_some_type::type some_type;
};
#包括
#包括
int main()
{

std::cout只要基类型定义了
some\u type
,这就可以工作,如果不这样做,您将得到一个编译器错误…试试这个:
struct somether\u base{
,那里没有定义
some_type
,因此上面的方法会失败。如果
some_base::some_type
没有定义,那么就省去第二个模板参数。
foo::some_type
将是
int
。我的解决方案不是完全自动的,但它是我唯一能想到的。这正是我所想的我在找!非常感谢。+1谢谢你的回答,我已经接受了高司令4的回答,正如你所说的,我认为这个答案是第一个(?)