1

我对下面的代码有两个问题:

为什么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。

4

1 回答 1

1
queue<T,  decreasing_order >uncoveredSetQueue( decreasing_order(tab) );/*2*/

最令人烦恼的 Parse的一个示例:它声明了一个名为的函数,该函数uncoveredSetQueue接受一个类型的参数(这里名为tabdecreasing_order并返回queue<T, decreasing_order>。正如您所发现的,添加括号可以避免最烦人的解析。您还可以将括号替换{}为使用统一初始化语法。

对我来说,clang 错误消息看起来像是一个编译器错误。很可能它不能正确处理使用本地类型作为模板参数的一些后果(这在 C++03 中是不允许的,并且是 C++11 的一个新特性)。

于 2014-03-28T14:06:45.557 回答