Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By iMalc
  • 1 Post By phantomotap

C++11 equivalent question

This is a discussion on C++11 equivalent question within the C++ Programming forums, part of the General Programming Boards category; I just received Bjarne Stroustrup's The C++ Programming Language fourth edition (C++11) I am not a seasoned c++ coder and ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    35

    C++11 equivalent question

    I just received Bjarne Stroustrup's The C++ Programming Language fourth edition (C++11)
    I am not a seasoned c++ coder and probably will be asking a number of questions in the future.
    I will not know if these are C++11 specific in probably most cases.
    I am trying to update a translator (basic to c/c++) to C++11 where posible.


    The translator produces this code
    Code:
       virtual const int   size(void)=0;
    The code I wish to represent is:
    Code:
       virtual int size() const = 0;
    Are these two lines equivalent ?


    MingGW g++ 4.8 compiles it fine but the proof is in the running and I'm not to that point yet.


    James

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > virtual const int size(void)=0;
    This tells you that no matter how many times you call size() on an object that hasn't changed, it will always give you the same answer.
    So things like
    for ( i = 0 ; i < foo.size() ; i++ )
    can be made especially efficient.

    > virtual int size() const = 0;
    This tells you that calling size does NOT change the object (though it says nothing about whether the answer may vary from one call to another).

    Neither of these things are C++11 specific.
    smokeyangel likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    No they are not.

    Code:
    virtual const int   size(void)=0;
    This one sets a return type of "const int". Which I don't think the compiler will care about too much. [edit -- wrong -- thanks Salem!] If you returned a pointer to a const int:
    Code:
    virtual const int *  size(void)=0;
    The compiler will complain at you if you try to discard the constness.

    Code:
    virtual int size() const = 0;
    This says that size is a const member function, meaning that it doesn't modify the class. So it can be legally called on a const this pointer, because that const keyword is your guarantee that it won't change the state of the class.

    So no... not equivalent. All I could find in the draft standard was a footnote about it being important.
    98) As indicated by syntax, cv-qualifiers are a signficant component in function return types.
    For the second
    In a const member function, the object for which the function is called is accessed through a const access
    path; therefore, a const member function shall not modify the object and its non-static data members.
    If the member function is declared const, the type of this is const X*,
    ^^ didn't know that,.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    35
    Thank you both. Looks like I need to do some parse mending.

    James

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by Salem View Post
    > virtual const int size(void)=0;
    This tells you that no matter how many times you call size() on an object that hasn't changed, it will always give you the same answer.
    That's not quite right.
    With the above, the const on the return type is redundant and useless. gcc in fact warns you that it is pointless for built-in types.
    Because this function signature doesn't specify that it doesn't itself modify the item, there can be no guarantees about the return value being the same each time it is called.

    Even if it were:
    Code:
    virtual const int size(void) const = 0;
    the const at the start is still useless. Perhaps you were thinking of const_expr, though I doubt can be used with virtual or pure virtual functions?
    King Mir likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by iMalc View Post
    ...though I doubt can be used with virtual or pure virtual functions?
    Yeah, it can't. It makes no sense.
    Virtual means that the function invocation will be at runtime, so the compiler wouldn't know which function to call at compile time anyway.

    EDIT:
    Section 7.1.5.3 in the C++14 draft says:
    Quote Originally Posted by C++14 standard draft
    The definition of a constexpr function shall satisfy the following constraints:
    — it shall not be virtual (10.3);
    Last edited by Elysia; 05-18-2013 at 05:03 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,145
    Virtual means that the function invocation will be at runtime, so the compiler wouldn't know which function to call at compile time anyway.
    O_o

    "...when used with code relying on polymorphic behaviors."

    There you go; that looks better.

    [Edit]
    Code:
    Derived s;
    s.go(); // The compiler knows that this is a `Derived' so is allowed to bypass lookup.
    [/Edit]

    Soma
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. equivalent to getch in c++
    By Elkvis in forum C++ Programming
    Replies: 1
    Last Post: 02-05-2009, 12:07 PM
  2. equivalent code question need help
    By Mshock in forum C Programming
    Replies: 4
    Last Post: 04-15-2006, 04:18 PM
  3. STL map<> Equivalent
    By nickname_changed in forum C# Programming
    Replies: 1
    Last Post: 02-27-2004, 05:32 AM
  4. %3s equivalent for C++
    By mackol in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2003, 05:10 AM
  5. what's the c equivalent to this asm code
    By *ClownPimp* in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2002, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21