c++ - 使用 constexpr-if 时出错 : expected '(' before 'constexpr'

标签 c++ c++17 if-constexpr

我正在尝试使用 if-constexpr 来检查某些内容,但我遇到了类似这样的错误

expected '(' before 'constexpr'

'else' without a previous 'if' "

到目前为止,我检查我的代码没有任何问题

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)会给出该错误,因为它们将 constexpr 识别为关键字但不理解constexpr-if 构造。确保您的 GCC 是版本 7 或更高版本。

关于c++ - 使用 constexpr-if 时出错 : expected '(' before 'constexpr' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291617/

相关文章:

c++ - 如何在 constexpr if-else 链中导致静态错误?

c++ - 为什么这个 constexpr if 不能编译

c++ - 当 ISAPI 筛选器实现 SF_NOTIFY_SEND_RAW_DATA 时,什么会导致吞吐量变得非常慢?

c++ - C++ 函数中的消息传递

linux - 在运行时确定 const char* 变量是否为字符串文字?

c++ - gcc 相对于在新表达式中解析 type-id 发出错误

c++ - 可以接收 T、T& 和 T&& 中的任何一个作为输入的函数,并且还可以识别其类型?

c++ - 使用 MinGW 64 安装 boost - (Cygwin)

c++ - 动态链接时 OpenCL 崩溃?

c++ - 根据模板参数不同的基类