下面的代码有什么问题?最新版本的 g++ 和 clang 都报错。我确定我在这里遗漏了一些基本的东西。
#include <iostream>
struct Z
{
static const int mysize = 10;
};
Z f2();
int main()
{
std::cout << f2()::mysize << std::endl;
}
这里的动机是能够使用以下代码使用模板找出数组的大小。我知道有很多方法,但偶然发现了这个想法。
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;
}