Issue
I am getting expected primary expression before constexpr in codeblocks only. Is there a way to fix this? This is the code I am trying to test. I have set c++ 17 as the compiler. I am using GNU GCC Compiler. GCC (MinGW.org GCC-6.3.0-1) 6.3.0. I am not getting this error in 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;
}
This is the error I am getting.
error: expected primary-expression before 'constexpr'
error: expected ')' before 'constexpr'
Solution
Your version of GCC does not support if constexpr
. Support was added in version 7.
You can look up which C++17 features are supported on the GCC website; the line relevant for this question is "constexpr if". There are a lot of "7"'s – and even one "8" – in the column for the first version that supports each feature, so you should expect C++17 support to be spotty with a compiler from the version 6 series (e.g. your 6.3.0).
Answered By - JaMiT Answer Checked By - Gilberto Lyons (WPSolving Admin)