Thread: Iterator quandary

  1. #16
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It seems to me that order of operation/precedence is being confused with order of evaluation. C++ clearly defines precedence but leaves order of evaulation up to the implementation in a lot of cases. So something as simple as int a = *p++; could be evaluated as if it were written as either int *tmp = p; ++p; int a = *tmp; or as int a = *p; ++p; (there is no externally visible difference between the two). Even things like the order that function call arguments are evaluated is implementation-defined.

    But something like Object a = *p++;, which overloads operator*, surely must be evaluated as if it were written as Object *tmp = p; ++p; Object a = *tmp;, right? Or does C++ leave that implementation-defined as well?

  2. #17
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by christop View Post
    It seems to me that order of operation/precedence is being confused with order of evaluation. C++ clearly defines precedence but leaves order of evaulation up to the implementation in a lot of cases. So something as simple as int a = *p++; could be evaluated as if it were written as either int *tmp = p; ++p; int a = *tmp; or as int a = *p; ++p; (there is no externally visible difference between the two). Even things like the order that function call arguments are evaluated is implementation-defined.

    But something like Object a = *p++;, which overloads operator*, surely must be evaluated as if it were written as Object *tmp = p; ++p; Object a = *tmp;, right? Or does C++ leave that implementation-defined as well?

    Well I couldn't get the same pointer-like behavior when defining a class and compiling under gcc/g++. (Rather unpleasant surprise, to say the least.) Which is why I decided to go with a different approach altogether. I wanted to make it easy for users to interface with my library. But yeah, just a subtle flaw in the language I guess.

  3. #18
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Sir Galahad View Post
    But yeah, just a subtle flaw in the language I guess.
    I wouldn't really call it a "flaw". It's just an implementation detail, and the application has to be written in a way that it doesn't care about the order an expression is evaluated.

  4. #19
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by christop View Post
    I wouldn't really call it a "flaw". It's just an implementation detail, and the application has to be written in a way that it doesn't care about the order an expression is evaluated.
    Should a "spade" be called by any other name? If a compiler can convert the code "*ptr++" to "*ptr; ++ptr;", it stands to reason it could do the same for objects which override those operators. As it is, the facility is almost useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::iterator::pointer and std::iterator::reference
    By etrusks in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2016, 11:27 AM
  2. Replies: 19
    Last Post: 01-06-2012, 03:01 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. std::map::iterator
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2006, 07:47 PM
  5. Hmm. A Quandary. Rounding/adding problem
    By Sennet in forum C++ Programming
    Replies: 11
    Last Post: 10-08-2005, 12:29 AM

Tags for this Thread