简体   繁体   English

c ++:是否有类似“boost / std typetraits conditional”的东西在编译时生成一个值(不是一个类型)?

[英]c++: Is there something like “boost/std typetraits conditional” that generates a value (not a type) at compile time?

I am currently doing the following to generate a value at compile time, which works: 我目前正在执行以下操作以在编译时生成一个值,该函数有效:

        //if B is true, m_value = TRUEVAL, else FALSEVAL, T is the value type
        template<bool B, class T, T TRUEVAL, T FALSEVAL>
        struct ConditionalValue
        {
            typedef T Type;

            Type m_value;

            ConditionalValue():
            m_value(TRUEVAL)
            {}
        };

        template<class T, T TRUEVAL, T FALSEVAL>
        struct ConditionalValue<false, T, TRUEVAL, FALSEVAL>
        {
            typedef T Type;

            Type m_value;

            ConditionalValue():
            m_value(FALSEVAL)
            {}
        };

Then you can simply do something like this: 然后你可以简单地做这样的事情:

template<class T>
void loadPixels(uint32 _w, uint32 _h, T * _pixels)
{
    PixelDataType::Type pixelType = PixelDataType::Auto; //enum I want to set

    ConditionalValue<boost::is_same<T, uint8>::value, PixelDataType::Type, PixelDataType::UInt8, PixelDataType::Auto> checker;
    pixelType = checker.m_value;

   ConditionalValue<boost::is_same<T, uint16>::value, PixelDataType::Type, PixelDataType::UInt16, PixelDataType::Auto> checker2;
   pixelType = checker2.m_value;

   ...

}

I know this example does not make much sense, but I use that code to set the value of an enum at compile time.- So here is my question: Is there something like that in std/boost type traits allready? 我知道这个例子没有多大意义,但我使用该代码在编译时设置枚举的值。所以这是我的问题: 在std / boost类型特征中是否有类似的东西? When browsing through the reference I only found conditional which does almost what I want, but only generates a type, not a value. 浏览引用时,我发现条件几乎完全符合我的要求,但只生成一个类型,而不是一个值。

EDIT: 编辑:

Updated example. 更新的例子。

Edit2: EDIT2:

I just realized that boost::is_same::value is all I need to solve my problem.- As for the answer to the question: There does not seem to be anything included in std/boost for good reason as pointed out by thiton 我刚刚意识到boost :: is_same :: value是解决我问题所需要的全部内容。至于问题的答案:std / boost中似乎没有任何东西包含在thiton中有充分理由

EDIT3: If you are still looking for a solution to create a value at compile time, you can either use my code wich works. 编辑3:如果您仍在寻找在编译时创建值的解决方案,您可以使用我的代码。 If you are looking for something very close to boost/stl Kerrek's or Nawaz seem to be valid solutions too. 如果你正在寻找非常接近boost / stl的东西KerrekNawaz似乎也是有效的解决方案。 If you are looking for a solution that assigns the correct enum at compile time Luc Touraille approach seems to be interesting even though i decided it's overkill for my situation! 如果你正在寻找一个在编译时分配正确枚举的解决方案, Luc Touraille的方法似乎很有意思,即使我认为这对我的情况来说太过分了!

A combination of std::conditional and std::integral_constant might work in some situations: std::conditionalstd::integral_constant可能在某些情况下有效:

template <bool B, typename T, T trueval, T falseval>
struct conditional_val : std::conditional<B,
       std::integral_constant<T, trueval>,
       std::integral_constant<T, falseval>>::type
{ };

Now use: 现在使用:

const int q = conditional_val<B, int, 12, -8>::value;

Equivalently: 等价的:

const int q = B ? 12 : -8;

Boost.MPL has a set of classes to manipulate data types at compile-time, along with some arithmetic operations. Boost.MPL有一组用于在编译时操作数据类型的类,以及一些算术运算。 These classes wrap a value into a type, for instance the integer 4 can be represented by the type mpl::int_<4> . 这些类将值包装到一个类型中,例如,整数4可以由类型mpl::int_<4>

You can use these in compile-time conditional: 您可以在编译时使用这些条件:

typedef typename 
    mpl::if_< 
        boost::is_same< T, uint8 >, 
        mpl::int_< 42 >, 
        mpl::int_< 187 >
    >::type result;

int i = result::value;

MPL also provides a generic integral wrapper that you can use with your enums: MPL还提供了一个通用的积分包装器 ,可以与枚举一起使用:

template<class T>
void loadPixels(uint32 _w, uint32 _h, T * _pixels)
{
    PixelDataType::Type pixelType = PixelDataType::Auto; //enum I want to set

    typedef typename mpl::if_<
        boost::is_same<T, uint8>, 
        mpl::integral_c<PixelDataType::Type, PixelDataType::UInt8>,
        mpl::integral_c<PixelDataType::Type, PixelDataType::Auto>
    >::type checker;

    pixelType = checker::value;

    typedef typename mpl::if_<
        boost::is_same<T, uint16>, 
        mpl::integral_c<PixelDataType::Type, PixelDataType::UInt16>,
        mpl::integral_c<PixelDataType::Type, PixelDataType::Auto>
    >::type checker2;

    pixelType = checker2::value;

    ...
}

If you have a lot of mapping like this to do, you could consider using a mixed compile-time/runtime data structure such as fusion::map , but that is probably a bit overkill :): 如果您有很多这样的映射要做,您可以考虑使用混合编译时/运行时数据结构,例如fusion :: map ,但这可能有点矫枉过正:):

typedef fusion::map<
    fusion::pair<uint8, PixelDataType::Type>,
    fusion::pair<uint16, PixelDataType::Type> >
map_type;

map_type pixelTypesMap(
    make_pair<uint8>(PixelDataType::UInt8),
    make_pair<uint16>(PixelDataType::UInt16));

...

template<class T>
void loadPixels(uint32 _w, uint32 _h, T * _pixels)
{
    // need special handling if T is not in the map
    PixelDataType::Type pixelType = fusion::at_key<T>(pixelTypesMap);

    ...
}

I think it's the simple answer: Because the operator ?: can select values quite well. 我认为这是一个简单的答案:因为运算符?:可以很好地选择值。 Types are harder to select, that's why boost constructs exist for that. 类型更难选择,这就是为什么存在boost构造的原因。 For pathological cases, the boost::mpl magic Luc suggested is fine, but it should be quite rare. 对于病理情况,boost :: mpl magic Luc建议很好,但它应该是非常罕见的。

I came across a case where I needed to do exactly what ? 我遇到了一个我需要做的事情? does (compare values, return values) but using template specialization (why would you ever need that? simple: compile time evaluation of ? can lead to "unreachable code" warnings) (比较值,返回值),但使用模板专门化(为什么你需要它?简单:编译时间评估?可能导致“无法访问的代码”警告)

So the most standard solution which works for your original question as well as for mine imho is: 因此,适用于您的原始问题以及我的imho的最标准解决方案是:

std::conditional<myval, 
                 std::integral_constant<T, val>, 
                 std::integral_constant<T, val>
                >::type::value;

Now simply replace "myval" with std::is_same and you have a solution for your case (compare types, return values), while above is a solution for my case (compare values, return values <=> ? ) 现在简单地用std::is_same替换“myval”并且你有一个解决方案(比较类型,返回值),而上面是我的情况的解决方案(比较值,返回值<=> ?

You can write yourself: 你可以写自己:

namespace extend 
{
  template<bool B, class T, T X, T Y>  
  struct conditional
  {
       static const T value = X;
  };
  template<class T, T X, T Y>  
  struct conditional<false,T,X,Y>
  {
       static const T value = Y;
  };
}

//test
assert(conditional<std::is_same<int,int>::value, int, 10, 20>::value == 10);
assert(conditional<std::is_same<int,char>::value, int, 10, 20>::value == 20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM