Here is a simple code that MSVC 2022 compiles in C++17 mode, but fails in C++20 mode:
template <typename T>
void foo()
{
std::basic_string<T>::size_type bar_size; //This fails to compile in C++20
}
The error being reported in C++20 mode does not help explain the reason: error C3878: syntax error: unexpected token 'identifier' following 'expression'
Interestingly, it only happens inside templated functions, this counterexample compiles fine in C++20 mode (as well as in C++17):
void baz()
{
std::basic_string<char>::size_type bar_size;
}
The only way I can fix the problem so far is to use auto instead of explicit data type, for example like so:
template <typename T>
void foo()
{
std::basic_string<T> bar;
auto bar_size = bar.size();
}
But I really want to learn what has changed in C++20 compared to C++17 that causes this syntax to be invalid instead of just mindlessly applying a workaround patch.
Use
typename std::basic_string<T>::size_type bar_size;
The name size_type
is a dependent name.