0

I have written this code to help me sort indices that refer to a collection, according to some predicate:

#include <algorithm>
#include <functional>
#include <vector>

template<template<class> class Pred = std::less>
struct element_is_pred
{
    template<class C>
    struct type : private Pred<typename C::value_type>
    {
        typedef Pred<typename C::value_type> Base;
        C const *c;
        type(C const &c, Base const &pred = Base())
            : Base(pred), c(&c) { }
        bool operator()(
            typename C::size_type const i,
            typename C::size_type const j) const
        { return this->Base::operator()((*c)[i], (*c)[j]); }
    };
};

template<template<class> class P, class C>
static element_is_pred<P>::template type<C const> element_is(
    C const &c,
    P<typename C::value_type> const &pred = P<typename C::value_type>())
{
    return typename element_is_pred<P>::template type<C const>(c, pred);
}

and I'm using it like this:

int main()
{
    std::vector<size_t> temp;
    std::vector<size_t> indices;
    indices.push_back(0);
    std::stable_sort(
        indices.begin(),
        indices.end(),
        element_is<std::less>(temp));
}

and when I compile it with Clang 3.2:

clang++ -fsyntax-only Test.cpp

it compiles fine.

But when I try to compile it with Visual C++ 2013, I get tons of errors, like:

test.cpp(23) : warning C4346: 'element_is_pred<Pred>::type<const C>' : dependent name is not a type
        prefix with 'typename' to indicate a type
test.cpp(23) : error C2146: syntax error : missing ';' before identifier 'element_is'
test.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Which compiler is correct?
What is the correct way to write the code to do this?

0

1 Answer 1

1

GCC gives the following error:

error: need 'typename' before 'element_is_pred<Pred>::type<const C>' because 'element_is_pred<Pred>' is a dependent scope

Following that advice, I can get the program to build on GCC by prepending typename:

static typename element_is_pred<P>::template type<C const> element_is(
       ^^^^^^^^

Clang allows the modified version as well.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for some reason this didn't work in my original code, but it works here. That'll lead me in the right direction, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.