Evaluation order for a=b

Discussion in 'C++' started by Xavier Decoret, Jul 3, 2003.

  1. Let's say I want assign a unique int id to a set of string. The
    following code is pretty compact and handy:

    deque<string> names; // I know there are unique

    map<string,int> idMap;
    for (deque<string>::const_iterator iter = names.begin();
    iter != names.end();++iter)
    {
    idMap[*iter] = idMap.size();
    }

    After that I can use the idMap to retrieve the unique id of a string.

    QUESTION:
    ---------

    On my implementation, the first string has id 1. I understand that
    idMap[*iter] is evaluated first so when the right hand term is
    evaluated, idMap do have a size() of 1.

    Can I trust this to be the case on any implementation or the order of
    evalutation for an assignement is not specified?

    --
    +-------------------------------------------------+
    | Xavier D├ęcoret - Post Doct |
    | Graphics Lab (LCS) - MIT |
    | mailto: |
    | home : http://www.graphics.lcs.mit.edu/~decoret|
    +-------------------------------------------------+
    Xavier Decoret, Jul 3, 2003
    #1
    1. Advertising

  2. Xavier Decoret

    Ron Natalie Guest

    "Xavier Decoret" <> wrote in message news:be1uov$2dlj$...

    >
    > Can I trust this to be the case on any implementation or the order of
    > evalutation for an assignement is not specified?
    >

    No. The order of operations in C++ is by and large unspecified.
    There's nothing that requires the lhs of the assignment to be evaluated
    before the right hand side or vice vera. You're going to have to introduce
    a temporary variable to be safe:

    int i = idMap.size();
    idMap[*iter] = i;
    Ron Natalie, Jul 3, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nan Li

    evaluation order

    Nan Li, Nov 14, 2005, in forum: Java
    Replies:
    11
    Views:
    651
    Oliver Wong
    Nov 15, 2005
  2. cheeser
    Replies:
    3
    Views:
    397
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Oct 5, 2003
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    391
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    757
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    333
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page