调用bitset模板参数的constexpr函数 [英] Calling constexpr function for bitset template parameter

查看:197
本文介绍了调用bitset模板参数的constexpr函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试输入 std :: bitset 类别的别名,其中模板参数N使用constexpr函数计算。

I'm trying to type alias the std::bitset class where the template parameter N is calculated using a constexpr function. However, this approach seems to be running into a wall.

代码现在看起来像这样:

The code currently looks like this:

static constexpr std::size_t ComponentCount() noexcept {
    return 3U;
}

static constexpr std::size_t TagCount() noexcept {
    return 5U;
}

using Bitset = std::bitset<ComponentCount() + TagCount()>;

我收到的错误如下:

1>error C2975: '_Bits': invalid template argument for 'std::bitset', expected compile-time constant expression
1>  note: see declaration of '_Bits'

感谢您的帮助。

推荐答案

事实证明,我没有在我的原始问题中包括足够的上下文。

As things turned out, I didn't include enough context in my original question. The problem ended up being a little more subtle.

这里是我的代码看起来更准确的代表:

Here's a more accurate representation of how my code looked:

template
<
    typename ComponentList,
    typename TagList,
    typename SignatureList
>
struct Settings {
    // ...

    static constexpr std::size_t ComponentCount() noexcept {
        return 3U;
    }

    static constexpr std::size_t TagCount() noexcept {
        return 5U;
    }

    // ...

    using Bitset = std::bitset<ComponentCount() + TagCount()>;

    // ...
};

这种方法对我来说似乎没问题,并没有提供任何编译器警告或任何东西。只是在原始问题中提到的编译器错误。

This approach seemed okay to me, and didn't provide me with any compiler warnings or anything. Just the compiler error mentioned in the original question.

但是,当我进一步简化问题,试图更准确地隔离问题,我结束了这: / p>

However, when I simplified the problem further in an attempt to more accurately isolate the problem, I ended up with this:

struct Settings {
    static constexpr std::size_t ComponentCount() noexcept {
        return 3U;
    }

    static constexpr std::size_t TagCount() noexcept {
        return 5U;
    }

    using Bitset = std::bitset<ComponentCount() + TagCount()>;
};

做这个简化之后(或者更确切地说,删除模板参数后),VS2015发现错误函数调用必须在 ComponentCount() > TagCount()函数调用,并以红色突出显示。显然,编译器不能查看静态constexpr函数包含在同一个结构体中的常量表达式?奇怪的。在定义const表达式之前可能尝试执行类型别名。

After doing this simplification (or more specifically, after removing the template parameters), VS2015 found the the error function call must have a constant value in a constant expression on both of the ComponentCount() and TagCount() function calls, and highlighted them in red. Apparently the compiler is unable to view static constexpr functions that are contained within the same struct as constant expressions? Weird. It might be trying to do the type aliasing before defining the const expressions.

模板化结构的解决方案如下:

The solution for the templated struct was as follows:

using ThisType = Settings<ComponentList, TagList, SignatureList>;

// ...

using Bitset = std::bitset<ThisType::ComponentCount() + ThisType::TagCount()>;

但是,这种方法不适用于非模板化结构。有关这种情况下的不同方法,请参阅我的其他 StackOverflow post

However, this approach doesn't work for the non-templated struct. See my other StackOverflow post for different approaches in that case.

这篇关于调用bitset模板参数的constexpr函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆