Empty constructor confusion

Discussion in 'C++' started by Daniel, Feb 4, 2014.

  1. Daniel

    Daniel Guest

    Environment is vs2010sp1 on Windows 7.

    Consider the following:

    template <typename C>
    class A
    {
    public:
    class B;

    A()
    {
    }

    A(const A& a)
    {
    }

    void f()
    {
    std::cout << "Hello world" << std::endl;
    }
    };

    template <typename C>
    class A<C>::B : public A<C>
    {
    public:
    B()
    {
    }
    B(size_t n)
    {
    }
    };

    If I write

    A<char> a(A<char>::B());
    a.f();

    compilation fails with "error C2228: left of '.f' must have class/struct/union"

    But if I change that to

    A<char> a(A<char>::B(10));
    a.f();

    compilation succeeds and the program runs.

    Hmmm ... am I doing something wrong?

    Thanks,
    Daniel
     
    Daniel, Feb 4, 2014
    #1
    1. Advertisements

  2. IIRC, it's interpreted as a declaration of a function 'a' that takes a
    function [pointer] as an argument and returns A<char>. The argument is
    a function with no arguments that returns a A<char>::B. Read up on
    "C++ most vexing parse".

    The solution is to surround the expression inside the parentheses in
    another set of parentheses:

    A<char> a((A<char>::B()));

    The extra parentheses force the compiler to interpret the inner
    expression as an expression instead of a declaration.
    Yes. You didn't read the FAQ before posting. Read the FAQ.

    V
     
    Victor Bazarov, Feb 4, 2014
    #2
    1. Advertisements

  3. How do new users actually know that there is an FAQ and where they can find
    it?
    (@daniel, it's: http://www.parashift.com/c++-faq/)

    Tobi
     
    Tobias Müller, Feb 4, 2014
    #3
  4. Daniel

    Öö Tiib Guest

    Typing "C++ FAQ" into google gives it first. C++ is unfortunately not
    too fitting tool for that subset of humankind who can't figure it out
    on their own.
     
    Öö Tiib, Feb 4, 2014
    #4
  5. Daniel

    Daniel Guest

    I am sorry that my efforts to find an answer myself were not successful, and for making you upset.

    Best regards,
    Daniel
     
    Daniel, Feb 4, 2014
    #5
  6. Daniel

    Daniel Guest

    It's curious that nobody seems to be asking questions on this newsgroup anymore. Could it be because everything is now in the FAQ? Or could it be
    because ...

    Best regards,
    Daniel
     
    Daniel, Feb 4, 2014
    #6
  7. Sure, if you know that an FAQ exists you should be able to find it. But for
    someone that is new to this group or even to the usenet it's not obvious.
    Do usenet groups usually have FAQs?

    And even if you are able to find it, it may be difficult to see if your
    question is actually part of it.

    Don't get me wrong, the FAQ is a good thing, but I don't see it as a hard
    requirement to read it before posting here.

    Tobi
     
    Tobias Müller, Feb 4, 2014
    #7
  8. Daniel

    Öö Tiib Guest

    Existence of FAQ for every non-silly and for lot of silly topics is a textual
    tradition that perhaps started from early mailing lists. For more important
    topics there are likely several FAQs.
    FAQ is helpful source of basic information. There are questions that everybody
    will ask sooner or later. Victor's suggestion to read FAQ was not rude (RTFM of
    early days of internet), it is fine idea.
    I do not argue there. I just said that C++ is unfortunately not too good tool
    for people whose mindset is not prepared for a territory full of bear traps
    and so do not value maps of it.

    For example the very "most vexing parse" of OP. It has been there as
    long there was C++. How many of us want to declare a function in body
    of other function? Close to zero. It is on close to all cases a typo. Our
    attitude is that we know what we are doing so only Clang and only quite
    recently has experimentally started to warn about it bit more clearly.
    It is hard road to find all such traps with trial and error.
     
    Öö Tiib, Feb 4, 2014
    #8
  9. Upset? Don't be silly. Frustration about inability to solve one's
    problem is often perceived by one as the most irritating, and some even
    see the need to ask for help as a sign of weakness. It cannot be
    helped, however, since it's all in one's head. And as they always say,
    if you've gained nothing, you at least have gained experience.
    Searching for answers elsewhere (which you supposedly have done) helps
    you develop and hone those investigative skills that you will use in
    your life and work for the years to come, I am sure.

    And now you know that there is such a thing as FAQ. Next time perhaps
    you will also look for FAQ in absence of a direct answer to your
    question, no matter what the venue.

    Best of luck!

    V
     
    Victor Bazarov, Feb 4, 2014
    #9
  10. Daniel

    Daniel Guest

    Thank you. And best of luck to you with your social skills!

    Daniel
     
    Daniel, Feb 4, 2014
    #10
  11. If you won't bother answer in a few lines, why don't you shut up and leave
    in silence. One must be a complete idiot to read a FAQ.
     
    Pierre Du Bois, Feb 4, 2014
    #11
  12. It most likely due to a completely different reason that you have to
    reply in that manner.
    Figures.

    V
     
    Victor Bazarov, Feb 4, 2014
    #12
  13. Daniel

    Stuart Guest

    That should have been all of Victor's answer.

    And that should have been your reply...


    IMNSHO, that advice is bullshit. After all, Daniel encountered some
    really disgusting compiler behaviour (I'd even call it a design error of
    the C++ language). How was he to know that the problem he encountered
    has already a name? Should he comb through the whole FAQ just to find
    out that his problem is already in there? And be honest, is this really
    a question that is so frequently asked in the newsgroup that it deserves
    a place in the FAQ? I think that Daniel's question was well worth
    reading and brought some information back from long-term memory.

    No offence, Victor. Your answer was - as usually - very informative.

    Regards,
    Stuart
     
    Stuart, Feb 4, 2014
    #13
  14. Daniel

    Daniel Guest

    Stuart, you're absolutely right, I should have ended it at that. I shouldn't have let Victor's condescending tone affect my appreciation for his quickresponse. I've followed his posts for many years, and I know he's always like that. The fact remains that he is very helpful, and it is appreciated.

    My apologies,
    Daniel
     
    Daniel, Feb 4, 2014
    #14
  15. Victor Bazarov, Feb 4, 2014
    #15
  16. Daniel

    Öö Tiib Guest

    Isn't it a bear trap that everybody should know of?

    Most C++ developers worth mentioning as such have met the issue in
    various forms and it deserves special name. Worst case of it is when
    someone thinks that he is taking some scoped lock or critical section
    when actually declaring a function. Then error is there silently waiting
    for race condition. It was one of the reasons behind yet another
    initialisation syntax 'Type variable{stuff, fluff};' introduced by C++11.
     
    Öö Tiib, Feb 4, 2014
    #16
  17. Daniel

    Daniel Guest

    Lots of web sites have a FAQ before a single question has been asked :)
    Yep. I knew about it 15-20 years ago. But in the context in which I was
    working, I just didn't see it.

    Daniel
     
    Daniel, Feb 4, 2014
    #17
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.