ARTICLE AD BOX
This code compiles correctly wit GCC, but it complains on MSVC:
template <typename T> std::ostream& raw_write(std::ostream& os, T& val, size_t size = sizeof(val)) { return os.write(reinterpret_cast<char*>(&val), size); }The error is:(5): error C2065: 'val': undeclared identifier.
Here is a demo.
This version instead works correctly:
template <typename T> std::ostream& raw_write(std::ostream& os, T& val, size_t size = sizeof(T)) { return os.write(reinterpret_cast<char*>(&val), size); }Is this another MSVC non compliance?
9
It is a bug of the MS C++ compiler. For example within the C++23 Standard in the section "9.3.4.7 Default arguments", p.#9 there is present the following example
9 A default argument is evaluated each time the function is called with no argument for the corresponding parameter. A parameter shall not appear as a potentially-evaluated expression in a default argument.
int h(int a, int b = sizeof(a)); // OK, unevaluated operand (7.2.3)Someday I tried the following simple function declaration in MS C++
void f( int x, size_t n = sizeof x );and I got an error saying that using a local variable as a default argument is not allowed.
Explore related questions
See similar questions with these tags.
