template <class T>
struct A {
template <class _T = T>
struct B {
static void func () {}
} ;
template <>
struct B<int> {
static void func () {}
} ;
static void func () {
B<>::func () ;
}
} ;
int main () {
A<int>::func () ;
return 0 ;
}
g++不允许这样写,那标准应该是怎么写的呢?
解决方案
5
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
namespace A {
template <class _T>
struct B {
static void func () {}
} ;
template <>
struct B<int> {
static void func () {}
} ;
static void func () {
B<int>::func () ;
}
}
using namespace A;
int main(int argc, char** argv) {
A::B<int>::func () ;
return 0;
}
5
函数应该用重载代替特化比较好
30
template <class T>
struct B {
static void func () {}
} ;
template <class T>
struct A {
static void func () {
B<int>::func () ;
}
} ;
int main () {
A<int>::func () ;
system("pause");
return 0;
}
这样不行么
20
放在匿名名空间里。
namespace
{
// your B stuff ...
}