Ad
Std::basic_string::size_type Causes Compile Error In C++20 Mode
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.
Ad
Answer
Use
typename std::basic_string<T>::size_type bar_size;
The name size_type
is a dependent name.
Ad
source: stackoverflow.com
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?
Ad