我对下面的代码有两个问题:
为什么version / * 1 * / 在g++下编译而version / * 2 * / 不行?
为什么这段代码不能在 clang 中编译?
我知道如何解决它。但我想了解为什么它不起作用。
#include <boost/test/unit_test.hpp>
template<typename T,typename Cmp>
class queue{
public:
queue(Cmp c):cmp(c){};
void push(T a){
cmp(a,a);
}
Cmp cmp;
};
template<typename T>
void fun(T a){
class decreasing_order
{
public:
decreasing_order(std::vector<T> BBB):AAA(BBB) {}
std::vector<T> AAA;
bool operator() (const T & lhs, const T & rhs) const
{
return AAA[lhs]<AAA[rhs];
}
};
std::vector<T> tab;
queue<T, decreasing_order >uncoveredSetQueue((decreasing_order(tab)));/*1*/
// queue<T, decreasing_order >uncoveredSetQueue( decreasing_order(tab) );/*2*/
uncoveredSetQueue.push(a);
}
BOOST_AUTO_TEST_CASE(TestX) {
fun(1);
}
在铿锵声中,我收到以下错误:
test.cpp:25:20: error:
'fun(int)::decreasing_order::operator()(const int &, const int
&)::decreasing_order::AAA' is not a member of class 'const
decreasing_order'
return AAA[lhs]<AAA[rhs];
^
/home/piotr/git/paal/test/greedy/test.cpp:10:9: note: in instantiation of
member function 'fun(int)::decreasing_order::operator()' requested
here
cmp(a,a);
^
/home/piotr/git/paal/test/greedy/test.cpp:30:23: note: in instantiation
of member function 'queue<int, decreasing_order>::push' requested
here
uncoveredSetQueue.push(a);
^
/home/piotr/git/paal/test/greedy/test.cpp:35:5: note: in instantiation of
function template specialization 'fun<int>' requested here
fun(1);
^
1 error generated.
我使用 g++ 4.8.1 和 clang 3.4-1。