c++ - 使用模板方法时出现 "expected primary expression"错误

标签 c++ templates c++11

我有一些实现 Pareto 规则的通用代码。它看起来像是格式正确的代码。

关于 newResult.set<Criterion>( criterion() ); 错误的 GCC 4.4 编译器消息表达。但我找不到问题。

完整错误日志:

trunk$ g++ -std=c++0x -o test test.cpp 
t6.cpp: In member function ‘bool Pareto<Minimize<T>, Types ...>::operator()(Map&, Map&)’:
t6.cpp:24: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘)’ token
t6.cpp:26: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘)’ token
t6.cpp: In member function ‘bool Pareto<Maximize<T>, Types ...>::operator()(Map&, Map&)’:
t6.cpp:43: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘)’ token
t6.cpp:45: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘)’ token

完整代码 list :

// TypeMap
template < typename ... Tail >
struct Holder;

template <typename ValueType, typename Head, typename ... Tail >
struct Holder<ValueType, Head, Tail ... > :
    public Holder<ValueType, Head>,
    public Holder<ValueType, Tail ... >
{};

template <typename ValueType, typename Head >
struct Holder<ValueType, Head>
{
    ValueType value;
};

template < typename ... Types >
struct TypeMap;

template <typename ValueType, typename ... Types >
struct TypeMap<ValueType, Types ... > :
    public Holder<ValueType, Types ... >
{
    template <typename T>
    void set(const ValueType& value)
    {
        ((Holder<ValueType, T>*)this)->value = value;
    }

    template <typename T>
    ValueType get()
    {
        return ((Holder<ValueType, T>*)this)->value;
    }
};

// Objectives
template <typename Criterion> struct Maximize : public Criterion {};
template <typename Criterion> struct Minimize : public Criterion {};

// Criteria
struct Criterion1{ double operator()(){ return 0; }};
struct Criterion2{ double operator()(){ return 0; }};

// Pareto rule
template < typename ... Types > struct Pareto;

template < typename T, typename ... Types >
struct Pareto<Minimize<T>, Types ... >
{
    template< typename Map >
    bool operator()(Map& oldResult, Map& newResult)
    {
        typedef Minimize<T> Criterion;
        Criterion criterion;

        // ERROR HERE !!!
        newResult.set<Criterion>( criterion() );

        if(newResult.get<Criterion>() >= oldResult.get<Criterion>())
            return false;

        Pareto<Types ... > pareto;
        return pareto(oldResult, newResult);
    }
};

template < typename T, typename ... Types >
struct Pareto<Maximize<T>, Types ... >
{
    template< typename Map >
    bool operator()(Map& oldResult, Map& newResult)
    {
        typedef Maximize<T> Criterion;
        Criterion criterion;

        // ERROR HERE !!!
        newResult.set<Criterion>( criterion() );

        if(newResult.get<Criterion>() <= oldResult.get<Criterion>())
            return false;

        Pareto<Types ... > pareto;
        return pareto(oldResult, newResult);
    }
};

template<>
struct Pareto<>
{
    template<typename Map>
    bool operator()(Map& oldResult, Map& newResult)
    {
        oldResult = newResult;
        return true;
    }
};

int main()
{
    TypeMap<double, Minimize<Criterion1>, Maximize<Criterion2>> oldResult, newResult;

    Pareto<Minimize<Criterion1>, Maximize<Criterion2>> pareto;
    pareto(oldResult, newResult);
}

最佳答案

找到了:

newResult.template set<Criterion>( criterion() );

if(newResult.template get<Criterion>() >= oldResult.template get<Criterion>())
    return false;

在这种情况下,您必须为编译器限定成员函数模板。 词法分析器无法决定(在模板声明时,而不是实例化)是否<Criterion表示模板参数列表 的开始,或者是比较运算符。

  • Using the template keyword as qualifier

  • What is the .template and ::template syntax about? (科莫)

  • 标准,§ 14.2,子 4.5.,值得注意:

    [ Note: As is the case with the typename prefix, the template prefix is allowed in cases where it is not strictly necessary; i.e., when the nested-name-specifier or the expression on the left of the -> or . is not dependent on a template-parameter, or the use does not appear in the scope of a template. —end note ]

关于c++ - 使用模板方法时出现 "expected primary expression"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8256636/

相关文章:

c++ - 为什么 defined(X) 在没有空格的预处理器定义中不起作用?

c++ - 包装模板函数和<未解析的重载函数类型

c++ - 使用 sfinae 在 clase 模板中选择不同的方法实现

c++ - 最大连续子数组(元素数量最多)

c++ - 编译带有 "gcc -c"的 C++ 程序顺利通过。为什么?

c++ - 使用继承命名空间的模板类进行名称查找

c++ - 多线程 c++11-ish 队列在 Windows 上失败

c++ - 如何创建函数指针数组?

c++ - 什么代表 C++ 中的 Math.IEEERemainder(x,y)?

c++ - 调用模板类成员时非法使用此类型作为表达式