I have the following array:
static std::pair<const Type, const size_t> typemap_[];
defined as
std::pair<const talos::Message::Type, const size_t> talos::Message::typemap_[8] = {
{ talos::Message::Type::Empty, typeid(int).hash_code() },
{ talos::Message::Type::Keyboard , typeid(int).hash_code() },
...
Why does this
sizeof(typemap_);
give a compile time error
Error C2070 'std::pair []': illegal sizeof operand
even though this
sizeof(typemap_[0]);
is legal and the array is of a fixed size?
Type is a defined as:
enum class Type {...}
Seems the compiler is missing the definition of the typemap_
variable. Since it's static you probably have it hidden in one of the source files.
If you put everything you have now into the same source the solution will work. For example:
enum class Type {None, Some} ;
static std::pair<const Type, const size_t> typemap_[] = {
{ Type::None, typeid(int).hash_code() },
{ Type::Some , typeid(int).hash_code() },
};
int main() {
std::cout << "sizeof: " << sizeof(typemap_) << " " << sizeof(typemap_[0]);
return 0;
}
Works well and outputs sizeof: 32 16
.
Same time sizeof
of a single element is legal since the compiler knows what the array consists of even without knowing its actual size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With