Instance of a non-templated class using templated constructor interpreted as a function definition?

后端 未结 2 1064
感情败类
感情败类 2021-01-20 12:20

My issue is best demonstrated by the following code:

#include 
#include 

class Bar
{
    public: template 

        
相关标签:
2条回答
  • 2021-01-20 12:56

    I think it is related to "C++ most vexing parse" that you will find in Meyer's Effective STL book.

     Bar bar(std::istream_iterator< char >(file), std::istream_iterator < char >()); 
    
    Is being considered as a **function declaration.**

    due to which in foo(bar); you are sending a function pointer instead :)

    Doing like below will have no error: Bar bar = Bar(//your arguments here); foo(bar);

    0 讨论(0)
  • 2021-01-20 13:15

    ArunMu gets partial credit, it is indeed an example of Most Vexing Parse, but that term was coined in Meyer's Effective STL (Chapter 1, Item 6) not Exceptional C++.

    It is being interpreted as a function pointer (the (__cdecl *) portion of the error is a dead give away), and apparently the C++ standard requires it to be interpreted that way. Does anyone have a chapter/verse citation for that?

    There is also a another solution to provide a disambiguation. Adding an additional set of parenthesis around each parameter works too:

    Bar bar( (std::istream_iterator<char>(file)), (std::istream_iterator<char>()) );
    

    It's also worth pointing out that the issue is unrelated to the templates, as I had originally thought.

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