怎么解决模板类内不能特化模板的问题

C++语言 码拜 8年前 (2016-05-08) 848次浏览
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

引用:
Quote: 引用:
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;

}

这样不行么

可以,谢了,不过怎么隐藏B类呢,本人不想B类的实现暴露

放在匿名名空间里。

namespace
{
// your B stuff ...
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么解决模板类内不能特化模板的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)