我想要使用helper struct和constepxr函数检查某些模板参数的有效性时遇到问题。只要没有引用静态constexpr成员,我想初始化编译器决定不评估表达式。我使用的代码如下:constexpr静态成员的强制评估
#include <cstddef>
#include <iostream>
#define CONSTEXPR static constexpr
using namespace std;
template<size_t ... Sizes>
struct _size_check_impl
{
static_assert(sizeof...(Sizes) != 0, "Dimension has to be at least 1");
CONSTEXPR size_t dimension = sizeof...(Sizes);
};
template<size_t ... Sizes>
constexpr size_t check_sizes()
{
return _size_check_impl<Sizes...>::dimension;
}
template<size_t ... Sizes>
struct Test
{
static constexpr size_t Final = check_sizes<Sizes...>();
};
int main()
{
Test<> a; // This shouldn't get through the static assert
Test<1, 2> b; // Passing
Test<2> c; // Passing
// cout << Test<>::Final; // With this it works just fine, bc Final is accessed
return 0;
}
有没有一种方法,我可以做到这一点,一些代理dependecy迫使编译器,如果constexpr进行评估,以评估值Final
?有没有另一种干净的方式来检查这个属性干净,快速?
原因是静态断言取决于'Sizes ...'参数,该参数直到两阶段查找的阶段2才被实例化。 – 0x499602D2
如果在任何地方使用'Test <...> :: Final',则会评估其值。如果没有,它不会。那么你不应该在乎它是否被评估。 –