Difficulty with "Local Types as Template Arguments"

Discussion in 'C++' started by towi, Feb 10, 2010.

  1. towi

    towi Guest

    Hello,

    I noticed that C++0x will allow the following:

    void func() {
    struct Op { // locally defined type
    void operator()(int arg) { /*...*/ }
    };
    std::for_each(..., Op()); // using Op as template argument
    }

    where before it was prohibited to use "Op" there because it is a
    template argument there.

    I wonder... why was this not included in C++98? What is/was the main
    challange in allowing this for compiler implementors?

    I have the feeling it has something to do with scopes an visibility of
    variables. Something like that in C (and C++98) averything a Functor/
    Function can operate on is visible at a global scope. Allowing this
    would introduce a complex scenario of making things visible, to where
    a Functor is called, what was initially only visible locally where the
    Functor is defined. I am aware that this is a challange in Pascal and
    a very distinguishing feature of Pascal versus C (and C++98).
    Is this correct, or are there other challanges to overcome -- or
    reasons why this was not included in C++98.

    Thanks in advance,

    tschau, towi.
     
    towi, Feb 10, 2010
    #1
    1. Advertising

  2. towi <> writes:

    > Hello,
    >
    > I noticed that C++0x will allow the following:
    >
    > void func() {
    > struct Op { // locally defined type
    > void operator()(int arg) { /*...*/ }
    > };
    > std::for_each(..., Op()); // using Op as template argument
    > }
    >
    > where before it was prohibited to use "Op" there because it is a
    > template argument there.
    >
    > I wonder... why was this not included in C++98? What is/was the main
    > challange in allowing this for compiler implementors?


    I believe it was excluded from C++98 "to be on the safe side" because
    implementors had concerns.

    e.g.

    void f()
    {
    struct X{ int i; double d;};
    std::vector<X> vec;
    }

    void g()
    {
    struct X{ std::string s;};
    std::vector<X> vec;
    }

    These two types X are different, and must yield distinct instantiations
    of std::vector<X>.

    Also

    inline void h()
    {
    struct X{};
    std::vector<X> vec;
    }

    This std::vector<X> must be the *same* instantiation for all translation
    units that include this definition of h().

    It was only included in C++0x because I and others campaigned to have it
    added. IIRC, it *will* cause a breaking change in the ABI for some
    compilers, but it is not the only C++0x feature to do so.

    Anthony
    --
    Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
    just::thread C++0x thread library http://www.stdthread.co.uk
    Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
    15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
     
    Anthony Williams, Feb 11, 2010
    #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. Bjoern Wolfgardt
    Replies:
    0
    Views:
    451
    Bjoern Wolfgardt
    Jul 16, 2003
  2. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    540
    Alvin Bruney
    Nov 30, 2003
  3. martinharvey via DotNetMonster.com

    Difficulty uploading information to database

    martinharvey via DotNetMonster.com, Dec 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    346
    martinharvey via DotNetMonster.com
    Dec 2, 2005
  4. Eric
    Replies:
    0
    Views:
    554
  5. Zenon
    Replies:
    4
    Views:
    674
    Zenon
    Nov 30, 2003
Loading...

Share This Page