What’s wrong with the code below? Latest version of g++ and clang both give error. I am sure I am missing something basic here.
#include <iostream>
struct Z
{
static const int mysize = 10;
};
Z f2();
int main()
{
std::cout << f2()::mysize << std::endl;
}
The motivation here is to be able to find out the size of an array using templates using code such as below. I know there are many ways, but just stumbled upon this idea.
template<int N> struct S
{
enum { mysize = N };
};
template<class T, int N> S<N> f(T (&)[N]);
int main()
{
char buf[10];
std::cout << f(buf)::mysize << std::endl;
}
f2()returns a value, not a type. You’d need to use the.operator on the return value instead of::The
::operator requires a type to be named on the lefthand side, while.allows for a value to be named. Your expressionf2()does not name a type so it cannot be used in conjunction with::.As a side note, with a little more detail in the question we might be able to solve your real problem.