jeudi 3 mai 2018

C++11 template parsing error, type deduction using template alias

I have a bit of a dumb question, template parsing errors are so common in stack overflow but I didn't find anything that was that close to my issue. I'll let someone else prove me wrong while I focus on this issue.

So I added a cryptic but simplified code snippet that duplicates what I'm trying to do.

#include <functional>
#include <iostream>

enum selector
{
    SEL1,
    SEL2,
};

I have trait classes that I am using to hold some "useful", repetitive and static information that I am trying to feed into an algorithm called f further down.

template <selector T>
class custom_traits;

template<>
class custom_traits<SEL1>
{
    public:
        template <typename T>
        using OPERATOR = std::less<T>;
};

template<>
class custom_traits<SEL1>
{
    public:
        template <typename T>
        using OPERATOR = std::greater<T>;
};

I have created a helper function to help do some operation that is unique to each trait class. I have copied and modified this function f() from cppreference since that is what I was using as a reference.

template <typename U, typename A, typename B>
bool f(A a, B b, U op = U()) 
{
    return op(a, b);
}

Here in my main function, let's call it g(), is where the template parsing error occurs when I make the call to f< TT::typename OPERATOR<OT> >(a,b)

template<  selector T, typename OT, typename TT = custom_traits<T> >
bool g( const OT a, const OT b)
{
    return f< TT::typename OPERATOR<OT> >(a,b);
}

int main() 
{
    g<SEL1>(1,3);

    return 0;
}

Interestingly enough it compiles just fine if I replace

f<TT::typename OPERATOR<OT>> 

with

f<std::less<int>> 

or even

f<std::less<OT>>

directly. So proof of concept it should work but the issue is having the template parsing to work for me. I originally thought that adding the typename would help the compiler figure out it is a type...but no luck.

It isn't very clear to me whether this type of deduction will even be possible w/template aliasing. Any assistance and pointers to more information would help.

Oh, this is my error by the way

 main.cpp: In function 'bool g(OT, OT)': main.cpp:39:12: error: parse
 error in template argument list
      return f< TT::typename OPERATOR<OT> >(a,b);

            ^

Aucun commentaire:

Enregistrer un commentaire