使用 constexpr-if 时出错:'constexpr'之前的预期'('
Errors when using constexpr-if: expected '(' before 'constexpr'
我试图使用if constexpr来检查某些内容,但我遇到了类似的错误
在'constexpr'之前应为"(">
没有前一个"if"的"else";
到目前为止,我检查我的代码没有任何问题
我的编译标志是g++-std=c++17 main.cpp
#include <iostream>
template<typename T, typename Comp = std::less<T> >
struct Facility
{
template<T ... list>
struct List
{
static void print()
{
std::cout<<"""<<"Empty List"<<"""<<"n";
}
};
template<T head,T ... list>
struct List<head,list...>
{
static void print()
{
std::cout<<""" << head;
((std::cout << " " << list), ...);
std::cout<<"""<<"n";
}
};
template<unsigned N,typename AA>
struct RemoveFirst{};
template<unsigned N,T head,T ... Rest>
struct RemoveFirst<N,List<head,Rest...>>
{
struct result
{
static void print()
{
if constexpr (N == head)
{
std::cout<<"";
}
else
{
std::cout<<"""<<head;
((std::cout << " " << Rest), ...);
std::cout<<"""<<"n";
}
}
};
};
};
template<int ... intlist>
using IntList = typename Facility<int>::List<intlist...>;
int main()
{
using IntFacility = Facility<int>;
using List = IntList<2, 8, 2, 3, 5, 10, 8, 5>;
}
不支持C++17最终版本的旧版本GCC(最高达6.x(会出现此错误,因为它们将constexpr
识别为关键字,但不理解constexpr-if构造。请确保您的GCC是版本7或更高版本。
相关文章:
- 在 constexpr 构造函数 (c++17) 中赋值到 const char * 在使用 Android NDK 时
- 静态数据成员:它"const declaration / constexpr definition"起作用?
- 为什么必须将 const 添加到 constexpr 中才能进行字符串文字声明?
- 为什么我不能声明一个 constexpr 本地,而一个 const 一个工作?
- MSVC使用constexpr-if从可变模板方法中的基本模板参数中吞下const
- 有什么陷阱用 constexpr string_view 替换全局 const char[]?
- const(但不是constexpr)用作内置数组大小
- 变量上的"const constexpr"是多余的吗?
- 将 'hana::string' 转换为 'constexpr const char (&)[]'
- 通过 constexpr 中的 'const char *' glvalue 访问字符串文字的值会出错
- constexpr 链表 - 从 const X* 到 X* 的转换无效
- 为什么 constexpr 假设我的方法就是 const
- 我应该更喜欢在函数中的常数:constexpr const或enum
- 预处理器检查 constexpr const char* 函数
- 继承自 std::true_type vs static constexpr const bool 成员
- constexpr const vs constexpr variables?
- 链接器错误(未定义的引用)与“静态 constexpr const char*”和完美转发
- constexpr vs const vs constexpr const
- constexpr const char * vs constexpr const char[]
- 为什么'constexpr const int &a = 1;'在块范围内失败了?