Arg*_*ent 5 c++ templates specialization template-specialization
编辑:当我去度假时,我实际上没有机会测试任何建议的解决方案,当我回来时,负责类模板的人做了一些改变,让我可以绕过需要使用类模板本身定义的类型.
感谢大家的帮助.
简而言之 - 并且随意纠正我的措辞,模板对我来说仍然有点巫术, - 我需要知道我是否可以在我的专业类中使用(protected)struct或#typedef定义的类模板.例如:
这是类模板:
template<typename T>
class A : public C<T>
{
protected:
struct a_struct { /* Class template implementation, doesn't depend on T */ };
void foo( a_struct a );
};
Run Code Online (Sandbox Code Playgroud)
我需要完全专注于T = VAL:
template<>
class A< VAL > : public C< VAL >
{
void foo( a_struct a )
{
// My implementation of foo, different from the class template's
}
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我做这样的事情,编译器抱怨a_struct在我的专门课程中未定义.我尝试了从类模板中专门化和继承,但是......太乱了.
我看到了一些解决方案,但所有这些都涉及修改类模板,这是我无法轻易做到的事情(不同的团队).
思考?
它并不漂亮,但你可以这样做:
template<typename T>
class C
{
};
template<typename T>
class A : public C<T>
{
protected:
friend A<int>;
// ^^^^^^
struct a_struct { /* Class template implementation, doesn't depend on T */ };
void foo( a_struct a );
};
template<>
class A< int > : public C< int >
{
using a_struct = typename A<void>::a_struct;
// ^^^^^^
void foo( a_struct a )
{
// My implementation of foo, different from the class template's
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |