Inject std namespace into main()

Discussion in 'C++' started by parmenides, Sep 11, 2013.

  1. parmenides

    parmenides Guest

    Hi,

    For the flowing code:

    #include <iostream>

    int cout;

    int main()
    {
    using namespace std;
    cout << endl;
    return 0;
    }

    I think all symbols in std are injected into main(), that is , the cout is a
    object local to the main(). It should shield the other cout in the global
    namespace. But, the complier has told me:

    reference to 'cout' is ambiguous

    Does the symbols of std is not local to the main()?
    parmenides, Sep 11, 2013
    #1
    1. Advertising

  2. On 11.09.2013 17:28, parmenides wrote:
    > Hi,
    >
    > For the flowing code:
    >
    > #include <iostream>
    >
    > int cout;
    >
    > int main()
    > {
    > using namespace std;
    > cout << endl;
    > return 0;
    > }
    >
    > I think all symbols in std are injected into main(), that is , the cout is a
    > object local to the main(). It should shield the other cout in the global
    > namespace. But, the complier has told me:
    >
    > reference to 'cout' is ambiguous
    >
    > Does the symbols of std is not local to the main()?


    The short version is that the `using` directive causes name lookup to
    look also in the specified namespace.

    Apparently you envision an effect as if there were a local declaration
    of `cout`. But the directive only adds additional name lookup places. A
    `using` DECLARATION, on the other hand, has that as-if-local-declaration
    effect, so that the above usage of `cout` (but not `endl`) will compile,
    and so that another local declaration would be invalid:

    Code:
    #include <iostream>
    
    int cout;
    
    int main()
    {
    using std::cout;    // A `using` declaration.
    cout << endl;
    }
    
    Note: the above fails to compile due to the use of unqualified `endl`.

    This is a main difference between a using DIRECTIVE (which you employed)
    and a using DECLARATION (above). The directive only directs lookup. The
    declaration acts more like a declaration. :)


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Oct 29, 2013
    #2
    1. Advertising

  3. parmenides

    SG Guest

    On Wednesday, October 30, 2013 12:27:49 AM UTC+1, Alf P. Steinbach wrote:
    >
    > This is a main difference between a using DIRECTIVE (which you employed)
    > and a using DECLARATION (above). The directive only directs lookup. The
    > declaration acts more like a declaration. :)


    As for how this "direction" works: It's like the names are injected
    into the closest namespace level that contains both, the context in
    which you are writing the directive and the namespace you are naming
    in the directive. So, for example:

    int i;
    namespace foo {
    namespace bar {
    int i;
    }
    namespace baz {
    using namespace bar; // puts the names from ::foo::bar
    // "temporarily" into ::foo because
    // foo is the "closest" namespace that
    // contains both bar and baz.

    void func() {i=23;} // i refers to ::foo::bar::i, it is
    // found when lookup reaches the foo
    // namespace.
    }
    void another() {i=42;} // i refers to ::i.
    }

    By "temporarily" I tried to refer to the scope in which the using
    directive is "active".

    HTH,
    SG
    SG, Oct 30, 2013
    #3
  4. parmenides

    SG Guest

    On Wednesday, September 11, 2013 5:28:23 PM UTC+2, parmenides wrote:
    > Hi,
    >
    > For the flowing code:
    >
    > #include <iostream>
    >
    > int cout;
    >
    > int main()
    > {
    > using namespace std;
    > cout << endl;
    > return 0;
    > }
    >
    > I think all symbols in std are injected into main(), that is , the cout is a
    > object local to the main(). It should shield the other cout in the global
    > namespace. But, the complier has told me:
    >
    > reference to 'cout' is ambiguous


    Right. std::cout and std::endl are found when lookup reaches the
    global namespace :):). But since there is already your int-variable
    with the same name (cout), you have the ambiguity.

    The names are NOT pulled into ::main. That's not how a using directive
    works. (see my previous post)

    If you want cout to be found when lookup checks the function-local
    scope, you need a using _declaration_ for that.
    SG, Oct 30, 2013
    #4
  5. parmenides

    SG Guest

    On Wednesday, October 30, 2013 3:56:27 PM UTC+1, SG wrote:
    >
    > Right. std::cout and std::endl are found when lookup reaches the
    > global namespace :):). But since there is already your int-variable
    > with the same name (cout), you have the ambiguity.
    >
    > The names are NOT pulled into ::main. That's not how a using directive
    > works. (see my previous post)
    >
    > If you want cout to be found when lookup checks the function-local
    > scope, you need a using _declaration_ for that.


    Keep in mind that names are searched starting from local scopes and
    going to the more global scopes. If/When one or more names are found
    in the scope that namelookup is currently focussing on, this kind of
    search stops and this is why names from inner scopes can "hide" names
    from outer scopes.

    In case the names found so far (if any) during unqualified lookup
    refer to class members, namelookup will stop. Otherwise, "associated"
    namespaces will be checked too (ADL, argument dependent lookup).

    I think that covers all the nuts and bolts of unqualified name lookup.

    HTH,
    SG
    SG, Oct 30, 2013
    #5
    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. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,353
    Howard
    Nov 29, 2004
  2. Replies:
    7
    Views:
    419
    Jacek Dziedzic
    Oct 3, 2006
  3. bb
    Replies:
    1
    Views:
    301
    Alf P. Steinbach
    Sep 7, 2007
  4. Peña, Botp

    inject does not inject last value

    Peña, Botp, Aug 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    178
    Peña, Botp
    Aug 7, 2006
  5. parmenides

    Inject std namespace into main()

    parmenides, Sep 11, 2013, in forum: C++
    Replies:
    2
    Views:
    134
    Nobody
    Sep 11, 2013
Loading...

Share This Page