1

Below code block won't compile when invoking template member function (on template parameter) without taking advantage template argument deduction. Can someone point out what I'm missing here, TIA.

struct Foo
{
    template<typename T>
    void set(const T& thing) { }
};

template<typename T>
class Bar 
{
public:
    Bar() {
        // struct Foo declared 
        f.set(5);       // compiles
        f.set<int>(5);  // compiles

        // struct Foo via template parameter type T
        t.set(5);       // compiles
        t.set<int>(5);  // doesn't compile
    }

    Foo f;
    T t;
};

int
main(int argc, char* argv[])
{
    Bar<Foo> b;
}

Code also available at http://coliru.stacked-crooked.com/a/50294c63b3acdef9

roalz
  • 2,699
  • 3
  • 25
  • 42
mr19
  • 187
  • 1
  • 15
  • 2
    Make it `t.template set(5)`. For all the compiler knows, `t.set` is a data member, and `<` that follows is a less-than operator. `template` keyword is necessary to tell the compiler that `set` is in fact a template. See also: http://stackoverflow.com/questions/30011740/the-template-disambiguator-for-dependent-names – Igor Tandetnik Feb 13 '16 at 04:32
  • @IgorTandetnik I think you should post that as an answer! – Curious Feb 13 '16 at 06:01
  • Thank you @IgorTandetnik! Makes complete sense. – mr19 Feb 13 '16 at 13:21

0 Answers0