The error of the expected primary expression in the template method using

I have a common code that implements the Pareto rule. This is like well-formed code.

GCC 4.4 compiler error messages for expression newResult.set<Criterion>( criterion() );. But I can not find the problem.

Full error log:

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

Full list of codes:

// 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);
}
+5
source share
1 answer

Found:

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

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

In this case, you need to qualify the member function templates for the compiler. The lexer will not be able to decide (during the declaration of the template, not the instantiation) whether the <Criterionbeginning of the list of template parameters means or instead the comparison operator.

Cm.

+8

All Articles