问题
I thought c++ concepts is a better method to write c++ templated code with better error messages and faster compile times , so I upgraded Visual Studio to 2019 and still waiting for clang to support concepts
however I tested some simple code with msvc from visual studio 2019 and g++ 8 from mingw-w64 and I'm having some trouble.
This is the test:
#include <iostream>
using namespace std;
// this compiles under g++ 8 but not visual studio 2019
template <class T>
bool concept CharT = std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
std::is_same_v<T, char16_t> || std::is_same_v<T, char32_t>;
// this compile under visual studio 2019 but not g++
//template <class T>
//concept CharT = std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
// std::is_same_v<T, char16_t> || std::is_same_v<T, char32_t>;
template <CharT char_type>
void PrintChar(char_type ch)
{
wcout << ch << endl;
}
int main()
{
PrintChar('c');
PrintChar(L'h');
PrintChar('a');
PrintChar('r');
}
Visual Studio invocation :
cl /std:c++latest concepts.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28316 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.
concepts.cpp
concepts.cpp(7): error C2988: unrecognizable template declaration/definition
concepts.cpp(7): error C2059: syntax error: 'concept'
concepts.cpp(7): fatal error C1903: unable to recover from previous error(s); stopping compilation
Internal Compiler Error in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\cl.exe. You will be prompted to send an error report to Microsoft later.
g++ invocation
g++ -std=c++2a -fconcepts concepts.cpp -o c.exe
concepts.cpp:12:9: error: 'CharT' does not name a type; did you mean 'char'?
concept CharT = std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
^~~~~
char
concepts.cpp:15:11: error: 'CharT' has not been declared
template <CharT char_type>
^~~~~
concepts.cpp:16:16: error: variable or field 'PrintChar' declared void
void PrintChar(char_type ch)
^~~~~~~~~
concepts.cpp:16:16: error: 'char_type' was not declared in this scope
concepts.cpp:16:16: note: suggested alternative: 'wchar_t'
void PrintChar(char_type ch)
^~~~~~~~~
wchar_t
concepts.cpp: In function 'int main()':
concepts.cpp:23:2: error: 'PrintChar' was not declared in this scope
PrintChar('c');
^~~~~~~~~
should the two syntax be supported ? or I'm using a deprecated or removed syntax with one compiler while the other doesn't support it ?
回答1:
The syntax with bool
is not C++20 concepts, but the syntax of the previous Concepts TS (Technical Specification). The latter is an experimental extension to C++17, superseded with C++20 concepts additions which are based on the TS, but made some changes to syntax and semantics.
GCC 8 only supports the TS, not C++20 concepts, you need GCC 10 for the latter.
See this cppreference.com page for documentation of the C++20 concepts additions and this one for the Concepts TS. See also this page for a list of compiler support for concepts (TS and C++20).
回答2:
As walnut says, concept bool
was the TS syntax while concept
is the C++20 syntax.
If you really need to support both compilers, you can use a feature test macro to select the correct syntax (although technically the Concepts TS isn't listed in that table):
#if __cpp_concepts >= 201707
// working paper, C++20 concepts
#define CONCEPT concept
#else
// TS
#define CONCEPT concept bool
#endif
template <typename T>
CONCEPT C = true;
This will work across all compilers that support concepts.
来源:https://stackoverflow.com/questions/60684179/visual-studio-2019-rejects-bool-concept-while-gcc-8-doesnt-compile-concepts-w