简体   繁体   English

使用constexpr方法在struct内部进行模板参数化

[英]Using constexpr method for template parameterization inside struct

This is a continuation of the problem I found and described here . 这是我在这里发现并描述的问题的延续。

Say you have a struct that contains a static constexpr function and a type alias for a std::bitset (or any type you wish to template using the result of the const expression) that looks as follows: 假设您有一个结构,其中包含一个static constexpr函数和一个std::bitset (或您希望使用const表达式的结果进行模板化的任何类型)的类型别名,如下所示:

struct ExampleStruct {
    static constexpr std::size_t Count() noexcept {
        return 3U;
    }
    using Bitset = std::bitset<Count()>;
};

Visual Studio 2015 version 14.0.25029.00 Update 2 RC highlights the Count() call in red and generates the error function call must have a constant value in a constant expression . Visual Studio 2015版本14.0.25029.00更新2 RC以红色突出显示Count()调用并生成错误function call must have a constant value in a constant expressionfunction call must have a constant value in a constant expression

How might one get this to compile, or achieve similar results? 一个人怎么可能将其编译或获得类似的结果?

What exactly is causing the error here? 到底是什么引起了错误? Is the compiler trying to generate the type alias before the const expression function? 编译器是否在const表达式函数之前尝试生成类型别名?

EDIT: The explanation for why this does not work can be found below, but since no one provided possible workarounds, here are some that I came up with: 编辑:为何找不到此功能的说明可在下面找到,但由于没有人提供可能的解决方法,因此我提出了一些解决方案:

(1) When using templates, store type alias to this type . (1)使用模板时,将类型别名存储为此类型

template<typename T>
struct ExampleStruct {
    using ThisType = ExampleStruct<T>;
    static constexpr std::size_t Count() noexcept {
        return 3U;
    }
    using Bitset = std::bitset<ThisType::Count()>;
};

(2) Move Count() function outside of the struct body. (2)将Count()函数移到结构体之外。

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

struct ExampleStruct {
    using Bitset = std::bitset<Count()>;
};

(3) Replace constexpr method with constexpr member variable. (3)用constexpr成员变量替换constexpr方法。

struct ExampleStruct {
    static constexpr std::size_t Count = 3U;
    using Bitset = std::bitset<Count>;
};

(4) Store value in constexpr member variable, and return this from Count() method. (4)将值存储在constexpr成员变量中,然后从Count()方法返回该值。

struct ExampleStruct {
private:
    static constexpr std::size_t m_count = 3U;
public:
    static constexpr std::size_t Count() noexcept {
        return m_count;
    }
    using Bitset = std::bitset<m_count>;
};

You might have noticed that if you move one or both lines outside of the class body, the error goes away. 您可能已经注意到,如果将一条或两条线移到类主体之外,该错误就会消失。 The problem you're running into is that class member function definitions (even inline ones) are not parsed until after the entire class definition has been parsed; 您遇到的问题是,只有在解析了整个类定义之后 ,才解析类成员函数定义(甚至是内联的)。 therefore, when the compiler sees using Bitset = std::bitset<Count()>; 因此,当编译器看到using Bitset = std::bitset<Count()>; , at that point Count has been declared but not yet defined, and a constexpr function that has not been defined cannot be used in a constant expression -- so you get the error you're seeing. ,此时Count已经声明但尚未定义,并且尚未定义的constexpr函数不能在常量表达式中使用-这样您会看到错误。 Unfortunately, I know of no good solution or workaround for this. 不幸的是,我没有很好的解决方案或解决方法。

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

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