使用constexpr-if时出错:在'constexpr'之前应为'(' [英] Errors when using constexpr-if: expected '(' before 'constexpr'
问题描述
我正在尝试使用if-constexpr进行检查,但是遇到诸如此类的错误
I am trying to use if-constexpr to check something, but I encounter errors like
期望'('在'constexpr'之前
expected '(' before 'constexpr'
'else',而前面没有'if'
'else' without a previous 'if' "
到目前为止,我检查我的代码是否有问题
So far i check there is nothing wrong with my codes
我的编译标志是g ++ -std = c ++ 17 main.cpp
My compiling flag is 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)将给出该错误,因为它们可以识别
Older versions of GCC (up to 6.x) which do not support the final version of C++17 will give that error, because they recognize constexpr
as a keyword but do not understand the constexpr-if construct. Make sure your GCC is version 7 or later.
这篇关于使用constexpr-if时出错:在'constexpr'之前应为'('的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!