c++ – Should I be able to use the size of another argument as a default value?


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?

Leave a Reply

Your email address will not be published. Required fields are marked *