Calling template function within template class

后端 未结 3 1270
無奈伤痛
無奈伤痛 2020-12-08 10:15

Disclaimer: The following question probably is so easy that I might be shocked seeing the first answer. Furthermore, I want to apologize for any duplicate questions - syntac

相关标签:
3条回答
  • 2020-12-08 10:33
    obj.bar<double>(1,2); // This line is faulty.
    

    The template keyword is required here, as obj is an instance of a type Foo<T> which depends on the template parameter T, and so the above should be written as:

    obj.template bar<double>(1,2); //This line is corrected :-)
    

    Read @Johannes's answer here for detail explanation:

    • Where and why do I have to put the "template" and "typename" keywords?
    0 讨论(0)
  • 2020-12-08 10:38

    As so often: Once the question was posted, the answer came all by itself. Correcting the faulty line to

    return obj.template bar<double>(1,2);
    

    yields the expected results.

    Thanks for reading...

    0 讨论(0)
  • 2020-12-08 10:51

    Could this be a case of foo.template bar‹ double >?

    0 讨论(0)
提交回复
热议问题