嵌套模板类的C ++非成员函数 [英] C++ non-member functions for nested template classes
问题描述
我写了几个类模板,其中包含嵌套迭代器类,需要进行等式比较。因为我相信是相当典型的,比较是用非成员(和非朋友)运算符==
函数执行。这样做,我的编译器(我使用Mingw32 GCC 4.4与标志 -O3 -g -Wall
)找不到该函数,我已经用尽了可能的原因。
I have been writing several class templates that contain nested iterator classes, for which an equality comparison is required. As I believe is fairly typical, the comparison is performed with a non-member (and non-friend) operator==
function. In doing so, my compiler (I'm using Mingw32 GCC 4.4 with flags -O3 -g -Wall
) fails to find the function and I have run out of possible reasons.
在下面相当大的代码块中有三个类:一个Base类,一个保存一个Base对象的Composed类和一个与Composed类相同的嵌套类除非它嵌套在一个外类中。提供了非成员运算符==
函数。这些类是模板化和非模板化的形式(在它们各自的命名空间中),后者相当于前者专门用于无符号整数。
In the rather large block of code below there are three classes: a Base class, a Composed class that holds a Base object, and a Nested class identical to the Composed class except that it is nested within an Outer class. Non-member operator==
functions are supplied for each. These classes are in templated and untemplated forms (in their own respective namespaces), with the latter equivalent to the former specialised for unsigned integers.
在 main
,比较每个类的两个相同的对象。对于未模拟的情况没有问题,但是对于模板化的情况,编译器无法找到 operator ==
。发生了什么?
In main
, two identical objects for each class are compared. For the untemplated case there is no problem, but for the templated case the compiler fails to find operator==
. What's going on?
#include <iostream>
namespace templated {
template<typename T>
class Base {
T t_;
public:
explicit Base(const T& t) : t_(t) {}
bool
equal(const Base& x) const {
return x.t_==t_;
}
};
template<typename T>
bool
operator==(const Base<T> &x, const Base<T> &y) {
return x.equal(y);
}
template<typename T>
class Composed {
typedef Base<T> Base_;
Base_ base_;
public:
explicit Composed(const T& t) : base_(t) {}
bool equal(const Composed& x) const {return x.base_==base_;}
};
template<typename T>
bool
operator==(const Composed<T> &x, const Composed<T> &y) {
return x.equal(y);
}
template<typename T>
class Outer {
public:
class Nested {
typedef Base<T> Base_;
Base_ base_;
public:
explicit Nested(const T& t) : base_(t) {}
bool equal(const Nested& x) const {return x.base_==base_;}
};
};
template<typename T>
bool
operator==(const typename Outer<T>::Nested &x,
const typename Outer<T>::Nested &y) {
return x.equal(y);
}
} // namespace templated
namespace untemplated {
class Base {
unsigned int t_;
public:
explicit Base(const unsigned int& t) : t_(t) {}
bool
equal(const Base& x) const {
return x.t_==t_;
}
};
bool
operator==(const Base &x, const Base &y) {
return x.equal(y);
}
class Composed {
typedef Base Base_;
Base_ base_;
public:
explicit Composed(const unsigned int& t) : base_(t) {}
bool equal(const Composed& x) const {return x.base_==base_;}
};
bool
operator==(const Composed &x, const Composed &y) {
return x.equal(y);
}
class Outer {
public:
class Nested {
typedef Base Base_;
Base_ base_;
public:
explicit Nested(const unsigned int& t) : base_(t) {}
bool equal(const Nested& x) const {return x.base_==base_;}
};
};
bool
operator==(const Outer::Nested &x,
const Outer::Nested &y) {
return x.equal(y);
}
} // namespace untemplated
int main() {
using std::cout;
unsigned int testVal=3;
{ // No templates first
typedef untemplated::Base Base_t;
Base_t a(testVal);
Base_t b(testVal);
cout << "a=b=" << testVal << "\n";
cout << "a==b ? " << (a==b ? "TRUE" : "FALSE") << "\n";
typedef untemplated::Composed Composed_t;
Composed_t c(testVal);
Composed_t d(testVal);
cout << "c=d=" << testVal << "\n";
cout << "c==d ? " << (c==d ? "TRUE" : "FALSE") << "\n";
typedef untemplated::Outer::Nested Nested_t;
Nested_t e(testVal);
Nested_t f(testVal);
cout << "e=f=" << testVal << "\n";
cout << "e==f ? " << (e==f ? "TRUE" : "FALSE") << "\n";
}
{ // Now with templates
typedef templated::Base<unsigned int> Base_t;
Base_t a(testVal);
Base_t b(testVal);
cout << "a=b=" << testVal << "\n";
cout << "a==b ? " << (a==b ? "TRUE" : "FALSE") << "\n";
typedef templated::Composed<unsigned int> Composed_t;
Composed_t c(testVal);
Composed_t d(testVal);
cout << "c=d=" << testVal << "\n";
cout << "d==c ? " << (c==d ? "TRUE" : "FALSE") << "\n";
typedef templated::Outer<unsigned int>::Nested Nested_t;
Nested_t e(testVal);
Nested_t f(testVal);
cout << "e=f=" << testVal << "\n";
cout << "e==f ? " << (e==f ? "TRUE" : "FALSE") << "\n";
// Above line causes compiler error:
// error: no match for 'operator==' in 'e == f'
}
cout << std::endl;
return 0;
}
推荐答案
嵌套类与模板。
template <class T>
struct Outer { struct Inner {}; };
template <class T>
void increment(typename Outer<T>::Inner&) {}
increment
无法找到函数。我认为查找太难了编译器来解决。
The increment
function cannot be found. I think the look up is too difficult for the compiler to solve.
你可以缓解这个问题,
namespace detail
{
template <class T> struct InnerImpl {};
template <class T> void increment(InnerImpl& ) {}
}
template <class T>
struct Outer
{
typedef detail::InnerImpl<T> Inner;
};
int main(int argc, char* argv[])
{
Outer<int>::Inner inner;
increment(inner); // works
}
很有趣,不是吗?
根据经验,自由方法的参数(不是结果类型)中的 typename
是一个红色的鲱鱼,似乎防止自动扣除参数。
As a rule of thumb, typename
in the arguments (not for the result type) of a free method is a red herring and seems to prevent automatic argument deduction.
这篇关于嵌套模板类的C ++非成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!