C++ “有没有办法执行?”;如果(条件)类型定义……”;

C++ “有没有办法执行?”;如果(条件)类型定义……”;,c++,c++11,templates,typedef,C++,C++11,Templates,Typedef,当且仅当满足编译时条件时,我想执行typedef。如果不满足该条件,则不应执行任何typedef 这在C++11中可能吗 例如: class A { std::conditional_typedef<true,int,myType1>; // Performs "typedef int myType1". std::conditional_typedef<false,int,myType2>; // Does nothing at all. }; A类{ std

当且仅当满足编译时条件时,我想执行
typedef
。如果不满足该条件,则不应执行任何
typedef

这在C++11中可能吗

例如:

class A {
  std::conditional_typedef<true,int,myType1>; // Performs "typedef int myType1".
  std::conditional_typedef<false,int,myType2>; // Does nothing at all.
};
A类{
std::conditional_typedef;//执行“typedef int myType1”。
std::conditional_typedef;//什么都不做。
};

我正在寻找这个虚构的
std::conditional_typedef

不幸的是,由于传递给模板实例化的名称必须已经定义,所以无法使用所需的语法。在您的情况下,
myType1
myType2
不会从编译器的角度命名任何东西。但是,如果您不坚持您提到的语法,您可以尝试使用
std::enable_if
,如下所示:

#include <type_traits>

struct A {
    struct myType1: std::enable_if<true, int> { }; //std::conditional_typedef<true,int,myType1>; // Performs "typedef int myType1".
    struct myType2: std::enable_if<false, int> { }; //std::conditional_typedef<false,int,myType2>; // Does nothing at all.

};

int main() {
    A::myType1::type i;
    //A::myType2::type i2; // causes error: no type named 'type' in 'A::myType2'
    (void)i;
}
#包括
结构A{
struct myType1:std::enable_if{};//std::conditional_typedef;//执行“typedef int myType1”。
struct myType2:std::enable_if{};//std::conditional_typedef;//什么都不做。
};
int main(){
A::myType1::i型;
//A::myType2::type i2;//导致错误:“A::myType2”中没有名为“type”的类型
(无效)i;
}


编辑:

我想到的另一种方法(使用默认模板参数):

#包括
结构A{
模板
使用myType1=typename std::enable_if::type;
模板
使用myType2=typename std::enable_if::type;
};
int main(){
A::myType1 i;
//A::MyType2J;
(无效)i;
}

另一种方法是从基类的专门化传递

// foo is a light struct (only a typedef or not at all) that can be
// developed in two versions

template <bool>
struct foo;

template <>
struct foo<true>
 { typedef int myType1; }; // or using myType1 = int;

template <>
struct foo<false>
 { };

template <bool B>
struct bar : public foo<B> // B can be a type_traits value derived
                           // from template arguments for bar
 {
   // potential complex struct or class, developed only one time 
 };


int main()
 {
   bar<true>::myType1 mt1 { 1 };
   // bar<false>::myType1 mt2 { 1 }; error: ‘myType1’ is not a member of ‘bar<false>’
 }
//foo是一个可以
//开发了两个版本
模板
结构foo;
模板
结构foo
{typedef int myType1;};//或者使用myType1=int;
模板
结构foo
{ };
模板
结构栏:public foo//B可以是派生的类型值
//来自bar的模板参数
{
//潜在的复杂结构或类,仅开发一次
};
int main()
{
bar::myType1 mt1{1};
//bar::myType1 mt2{1};错误:“myType1”不是“bar”的成员
}

类似于std::enable\u,如果您想使用自己的名称而不必执行::mytype1::type,则可以让类从执行typedef的模板派生。缺点是,如果您想经常使用多个结构,就必须从多个结构中进行降级

namespace impl {
    template<bool, typename> struct A;
    template<typename T> struct A<true, T>{ typedef T mytype1; };
    template<typename T> struct A<false, T> {};
}

struct A : public impl::A<condition, int> {
    //If condition is met, then ::mytype1 will be defined as a typedef int
};

int main() {
    A::mytype1 i; //Will fail if condition wasn't met
}
namespace impl{
模板结构A;
模板结构A{typedef T mytype1;};
模板结构A{};
}
结构A:public impl::A{
//如果满足条件,那么::mytype1将被定义为typedef int
};
int main(){
A::mytype1 i;//如果不满足条件,则将失败
}

如果(…)typedef…,你能提供一些理论上的
示例代码吗?特别是,如果没有“执行”typedef,那么使用typedef的代码会发生什么情况?根据条件专门化可能就足够了…一个简单的解决方案是有条件地将typedef类型化为请求的类型,或者是在大多数情况下无法使用的内容,例如
void
。这适合你的情况吗?有一种古老的
#ifdef
?使用继承似乎是解决这个问题的一种非常自然的方法。不能没有派生的
,直接使用
foo::myType1 mt1{1}
?@chi-可以做到;使用基类(IMHO)的优点是,您可以使用(或不使用)typedef开发简单类和轻型类的两种不同专门化;这允许开发一个可能很复杂的派生类的单一版本。@max66:即使在技术上使用继承,我还是会将其标记为mix-in。这是模板中非常自然的解决方案。@MatthieuM.-好。。。我不是一个专家,我在标签方面很差,但是,是的,使用模板我觉得它很自然。
namespace impl {
    template<bool, typename> struct A;
    template<typename T> struct A<true, T>{ typedef T mytype1; };
    template<typename T> struct A<false, T> {};
}

struct A : public impl::A<condition, int> {
    //If condition is met, then ::mytype1 will be defined as a typedef int
};

int main() {
    A::mytype1 i; //Will fail if condition wasn't met
}