1

So, I reduce down some code I have to this :

#include <iostream>
#include <functional>

using namespace std;

struct Test {
    template <class T>
    void operator()() {
        cout << typeid(T).name() << endl;
    }
    template <class T>
    void f() {
        cout << typeid(T).name() << endl;
    }
};

template <typename S, typename T, typename T1>
mem_fun_ref_t<S, T> make_mem_fun_ref()
{
    return mem_fun_ref(&T::template operator()<T1>); //A
    //return mem_fun_ref(&T::template f<T1> ); //B
}

int main(int argc, char *argv[]) {
    Test t;
    make_mem_fun_ref<void, Test, int>()(t);
}

This compiles just fine under gcc, but VS2013 complains about : "error C2902: 'operator' : unexpected token following 'template', identifier expected".

Now, if I comment line A and use line B, VS does not complain anymore.

Is that a bug related to the interpretation of the template keyword with the operator() ? Should I change the syntax or enable some magic flag/pragma to compile line A ?

BTW: I need to have a mem_fun_ref_t that refers to a templated member operator() of some functor. Before anyone suggests it, I can't use any C++11 or Boost code.

Thanks

  • I'm not sure it makes much sense to have an operator function template when the template parameters can't be deduced... – T.C. Sep 21 '14 at 09:07
  • This looks like a [pretty old VC++ bug](https://connect.microsoft.com/VisualStudio/feedback/details/529349/bd-c2902-pi-vyvo-aniu-operatora-vzortzevego). If the template parameters can be deduced from the parameter types, consider using a `static_cast*...*/>(&T::operator())` to the appropriate pointer-to-member-function type to activate template argument deduction instead. – T.C. Sep 21 '14 at 09:25
  • Why do you have the template keyword in front of the operator keyword? Without this it is compiling fine in VS (but not in gcc). – ConfusedSushi Sep 21 '14 at 13:48
  • @ConfusedSushi That's [required](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) because the compiler is supposed to check syntax at template definition time, when it doesn't know what `T` is and hence whether `T::operator()` is a template and hence how to interpret the following `<`. MSVC's template engine is nonconforming in numerous ways, this being one of them. – T.C. Sep 22 '14 at 09:02
  • Did you ever figure this out? – Korijn Sep 07 '16 at 16:08

0 Answers0