问题描述
我试图使NCORR库在VS2015 C ++上工作。
I try to make NCORR library work on VS2015 C++. The library was originally written on Linux.
作为库的一部分,有类似于
As part of the library there is code similar to
#include <chrono>
template<typename T_container>
struct container_traits {
typedef typename T_container::container nonconst_container;
};
template <typename T_container>
class base_region {
public:
typedef typename container_traits<T_container>::nonconst_container nonconst_container;
template<typename T_container2>
typename std::enable_if<std::is_same<typename container_traits<T_container2>::nonconst_container, typename nonconst_container>::value, base_region&>::type operator=(const base_region<T_container2>&);
};
template <typename T_container>
template <typename T_container2>
typename std::enable_if<std::is_same<typename container_traits<T_container2>::nonconst_container, typename base_region<T_container>::nonconst_container>::value, base_region<T_container>&>::type base_region<T_container>::operator=(const base_region<T_container2>& reg) {
return *this;
}
int main()
{
return 0;
}
当我尝试在VS2015 win32控制台应用程序上编译代码时, / p>
When I try to compile the code on VS2015 win32 console application, I get
C2244 'base_region<T_container>::operator =': unable to match function definition to an existing declaration ConsoleApplication5
我认为问题是 typename nonconst_container
$ c> typename base_region< T_container> :: nonconst_container 在定义中。
I think the problem is typename nonconst_container
in declaration vs typename base_region<T_container>::nonconst_container
in definition.
你知道什么是错的, ?
Do you know what is wrong and how can I make the code work?
推荐答案
首先, typename nonconst_container
typename
只允许在限定名称之前。
First, typename nonconst_container
is ill-formed. typename
is only allowed before a qualified name.
MSVC似乎有匹配 nonconst_container在类模板定义中的
和在类外成员函数定义中的 typename base_region< T_container> :: nonconst_container
。
MSVC seems to have problems matching nonconst_container
in the class template definition and the typename base_region<T_container>::nonconst_container
in the out-of-class member function definition.
解决方法(也较短)是使用 trailing-return-type :
A workaround (which is also shorter) is to use a trailing-return-type:
template <typename T_container>
template <typename T_container2>
auto base_region<T_container>::operator=(const base_region<T_container2>& reg)
-> typename std::enable_if<std::is_same<typename container_traits<T_container2>::nonconst_container,
nonconst_container>::value, base_region&>::type
// ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
{
//...
}
base_region< T_container> :: operator =
之后的所有内容在成员函数体中使用(特别是,它将首先查看类成员),所以你可以写 nonconst_container
和 base_region&
,并愉快地回避MSVC错误。
Everything after base_region<T_container>::operator=
gets looked up just like a name used in the member function body (in particular, it will first look at class members), so you can just write nonconst_container
and base_region&
, and happily sidestep the MSVC bug.
这篇关于VS2015 C ++:无法将函数定义与现有声明匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!