【发布时间】:2011-12-13 07:18:25
【问题描述】:
我有一个包含两个相似成员函数的类模板:
template<class T>
class MyTemplate {
// other stuff, then
static T* member( T& ); //line 100
static const T* member( const T& ); //line 101
};
我这样实例化:
MyTemplate<int* const>
Visual C++ 9 抱怨:
mytemplate.h(101) : error C2535: 'int* MyTemplate<T>::member(T &)' :
member function already defined or declared
with
[
T=int *const
]
mytemplate.h(100) : see declaration of 'MyTemplate::member'
with
[
T=int *const
]
somefile.cpp(line) : see reference to class template instantiation
'MyTemplate<T>' being compiled
with
[
T=int *const
]
我当然需要member() 的两个版本——一个用于常量引用,一个用于非常量引用。我猜这个问题与顶级 const 限定符有关,但无法推断出如何解决它。
如何解决这个问题,以便我仍然有两个版本的member() 并且模板可以编译?
【问题讨论】:
标签: c++ templates visual-c++ constants