首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用constexpr时的错误-如果:预期'(‘在'constexpr’之前‘

使用constexpr时的错误-如果:预期'(‘在'constexpr’之前‘
EN

Stack Overflow用户
提问于 2018-07-11 17:53:26
回答 1查看 3.3K关注 0票数 8

我正在尝试使用if-constexpr来检查某些东西,但是我遇到了如下错误

预期“(”在“警察”之前) “否则”而没有先前的“如果”“

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

我的编译标志是g++ -std=c++17 main.cpp

代码语言:javascript
复制
#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>;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 18:36:32

GCC的旧版本(最多6.x)不支持C++17的最终版本,这将导致该错误,因为它们将constexpr识别为关键字,但不理解constexpr if结构。确保你的GCC是第7版或更高版本。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51291617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档