使用 constexpr-if 时出错:在 'constexpr' 之前应为“(”
Errors when using constexpr-if: expected '(' before '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>;
}
旧版本的 GCC(直到 6.x)不支持最终版本的 C++17 会给出该错误,因为它们将 constexpr
识别为关键字但不理解constexpr-if 结构。确保您的 GCC 是版本 7 或更高版本。
我正在尝试使用 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>;
}
旧版本的 GCC(直到 6.x)不支持最终版本的 C++17 会给出该错误,因为它们将 constexpr
识别为关键字但不理解constexpr-if 结构。确保您的 GCC 是版本 7 或更高版本。