为什么使用 "constexpr" 会在代码块中产生主要表达式错误
Why does using "constexpr" give primary expression error in codeblocks
我仅在代码块中获得 constexpr 之前的预期主表达式。有没有办法来解决这个问题?这是我要测试的代码。我已将 c++ 17 设置为编译器。我正在使用 GNU GCC 编译器。海湾合作委员会(MinGW.org GCC-6.3.0-1)6.3.0。我在 Visual Studio.
中没有收到此错误
#include <iostream>
#include <vector>
template<class T>
void testType(std::vector<T> &x)
{
if constexpr (std::is_same_v<T, std::string>)
{
//push string
std::cout<<"String\n";
}
else if constexpr (std::is_same_v<T,int>)
{
//push integer
std::cout<<"int\n";
}
}
int main()
{
std::vector<std::string> x;
testType(x);
return 0;
}
这是我遇到的错误。
error: expected primary-expression before 'constexpr'
error: expected ')' before 'constexpr'
您的 GCC 版本不支持 if constexpr
。版本 7 中添加了支持。
您可以查看支持哪些 C++17 功能on the GCC website;与这个问题相关的行是“constexpr if”。在支持每个功能的第一个版本的列中有很多“7”——甚至一个“8”,所以你应该期望 C++17 支持与版本 6 系列的编译器参差不齐(例如您的 6.3.0)。
我仅在代码块中获得 constexpr 之前的预期主表达式。有没有办法来解决这个问题?这是我要测试的代码。我已将 c++ 17 设置为编译器。我正在使用 GNU GCC 编译器。海湾合作委员会(MinGW.org GCC-6.3.0-1)6.3.0。我在 Visual Studio.
中没有收到此错误#include <iostream>
#include <vector>
template<class T>
void testType(std::vector<T> &x)
{
if constexpr (std::is_same_v<T, std::string>)
{
//push string
std::cout<<"String\n";
}
else if constexpr (std::is_same_v<T,int>)
{
//push integer
std::cout<<"int\n";
}
}
int main()
{
std::vector<std::string> x;
testType(x);
return 0;
}
这是我遇到的错误。
error: expected primary-expression before 'constexpr'
error: expected ')' before 'constexpr'
您的 GCC 版本不支持 if constexpr
。版本 7 中添加了支持。
您可以查看支持哪些 C++17 功能on the GCC website;与这个问题相关的行是“constexpr if”。在支持每个功能的第一个版本的列中有很多“7”——甚至一个“8”,所以你应该期望 C++17 支持与版本 6 系列的编译器参差不齐(例如您的 6.3.0)。