问题描述
我一直在试图做这个简单的东西,Visual Studio 2008似乎不喜欢。
template< ; class CharType>
class SomeClass
{
public:
template< class T1,class T2>
static bool SomeOperator(const typename T1 :: const_iterator& p_Begin1,
const typename T1 :: const_iterator& p_End1,
const typename T2 :: const_iterator& p_Begin2,
const typename T2 :: const_iterator& p_End2)
{
//做一些东西...
}
};
并使用以下命令:
std :: wstring a;
OtherString b;
SomeClass< wchar_t> :: SomeOperator(a.begin(),a.end(),b.begin(),b.end());
我得到的是编译器错误,说明它不能推导模板参数T1和T2
错误C2783:'bool SomeClass< CharType> :: SomeOperator(const T1 :: const_iterator&,const T1 :: const_iterator& const T2 :: const_iterator&,const T2 :: const_iterator&)':无法推导出'T1'的模板参数
pre>
error C2783:'bool SomeClass< CharType> :: SomeOperator(const T1 :: const_iterator& ; const T1 :: const_iterator&,const T2 :: const_iterator&,const T2 :: const_iterator&)':无法推导出'T2'的模板参数
解决方案编译器根本无法从上下文中推导出类型。
假设
std :: wstring :: const_iterator
实际上是const wchar_t *
,这很可能。在这种情况下,编译器如何知道它应该替换std :: wstring
,而不是任何其他类型T
code> T :: const_iterator 为const wchar_t *
(可能向量< wchar_t>
)?
编译器不可能准确地告诉你。由于类似的原因,您不能在函数调用中推导出
some_template< T> :: type
。
解决方法很容易。你实际上不需要容器类型 - 迭代器类型上的模板可以正常工作:
template< typename I1,typename I2>
static bool SomeOperator(const I1& p_Begin1,const I1& p_End1,
const I2& p_Begin2,const I2& p_End2)
{/ * stuff * /}
如果你发现自己在需要容器类型的情况下,你将不得不传递容器或明确指定类型在函数调用中。
I've been trying to do this simple stuff and Visual studio 2008 does not seems to like it.
template <class CharType> class SomeClass { public: template <class T1, class T2> static bool SomeOperator(const typename T1::const_iterator& p_Begin1, const typename T1::const_iterator& p_End1, const typename T2::const_iterator& p_Begin2, const typename T2::const_iterator& p_End2) { // do some stuff.. } };
And call it with something like this:
std::wstring a; OtherString b; SomeClass<wchar_t>::SomeOperator(a.begin(), a.end(), b.begin(), b.end());
What I get is compiler errors stating that it can't deduce template parameter T1 and T2
error C2783: 'bool SomeClass<CharType>::SomeOperator(const T1::const_iterator &,const T1::const_iterator &,const T2::const_iterator &,const T2::const_iterator &)' : could not deduce template argument for 'T1' error C2783: 'bool SomeClass<CharType>::SomeOperator(const T1::const_iterator &,const T1::const_iterator &,const T2::const_iterator &,const T2::const_iterator &)' : could not deduce template argument for 'T2'
解决方案The compiler is simply unable to deduce types from this context.
Suppose
std::wstring::const_iterator
is actuallyconst wchar_t*
, which is likely. In that case, how does the compiler know it should substitutestd::wstring
rather than any other typeT
withT::const_iterator
beingconst wchar_t*
(perhapsvector<wchar_t>
)?It's impossible for the compiler to tell exactly. For similar reasons, you cannot deduce
some_template<T>::type
in function calls.In your case, the workaround is easy. You don't actually need the container type - templating on the iterator types will work fine:
template <typename I1, typename I2> static bool SomeOperator(const I1& p_Begin1, const I1& p_End1, const I2& p_Begin2, const I2& p_End2) { /* stuff */ }
If you find yourself in a situation where you need the container type, you will have to either pass the container around or explicitly specify the type in the function call.
这篇关于模板类中的依赖类型参数的模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!