template instantiation with local type

Discussion in 'C++' started by Neelesh Bodas, Dec 9, 2005.

  1. Hello all,

    Is there a way to pass an object of local type to a template?

    template <class T> int fun(const T& x) { return 1; }

    int bar()
    {
    enum Color { Red, Green, Blue};
    Color c;
    fun(c); // Error, Color is local to bar()
    }
     
    Neelesh Bodas, Dec 9, 2005
    #1
    1. Advertisements

  2. Rolf Magnus wrote:
    > No. If you want to use a type as template argument, that type must have
    > external linkage, and local types don't have any linkage.


    Great. Thanks.
     
    Neelesh Bodas, Dec 9, 2005
    #2
    1. Advertisements

  3. Neelesh Bodas

    Rolf Magnus Guest

    Neelesh Bodas wrote:

    > Hello all,
    >
    > Is there a way to pass an object of local type to a template?


    No. If you want to use a type as template argument, that type must have
    external linkage, and local types don't have any linkage.
     
    Rolf Magnus, Dec 9, 2005
    #3
  4. Neelesh Bodas

    Greg Guest

    Neelesh Bodas wrote:
    > Hello all,
    >
    > Is there a way to pass an object of local type to a template?
    >
    > template <class T> int fun(const T& x) { return 1; }
    >
    > int bar()
    > {
    > enum Color { Red, Green, Blue};
    > Color c;
    > fun(c); // Error, Color is local to bar()
    > }


    No. The usual workaround is to place the declarations in an anonymous
    namepspace:

    template <class T>
    struct TemplateClass
    {
    };

    namespace
    {
    enum Color { Red, Green, Blue};
    Color c;

    }

    int main()
    {
    TemplateClass<file_scope_type> a;

    //...
    }

    An anonymous namespace essentially allows local types at the file level
    to be used as template type parameters.

    Greg
     
    Greg, Dec 10, 2005
    #4
  5. Neelesh Bodas

    Greg Guest

    Neelesh Bodas wrote:
    > Hello all,
    >
    > Is there a way to pass an object of local type to a template?
    >
    > template <class T> int fun(const T& x) { return 1; }
    >
    > int bar()
    > {
    > enum Color { Red, Green, Blue};
    > Color c;
    > fun(c); // Error, Color is local to bar()
    > }


    No. The usual suggestion is to place the declarations in an anonymous
    namespace:

    template <class T> int fun(const T& x) { return 1; }

    namespace {
    enum Color { Red, Green, Blue};
    }

    int main()
    {
    Color c;

    fun(c); // OK, Color is unique to local file
    }

    An anonymous namespace essentially allows local types at the file level
    to be used as template type parameters.

    Greg
     
    Greg, Dec 10, 2005
    #5
    1. Advertisements

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. Marcin Kalicinski
    Replies:
    5
    Views:
    517
    Buster
    Apr 2, 2004
  2. Fernando Cuenca
    Replies:
    4
    Views:
    2,752
    Gianni Mariani
    Sep 6, 2004
  3. Mikhail N. Kupchik
    Replies:
    4
    Views:
    3,420
    Jonathan Turkanis
    Sep 15, 2004
  4. Thomas Maier-Komor
    Replies:
    6
    Views:
    872
    Thomas Maier-Komor
    May 19, 2005
  5. Replies:
    1
    Views:
    823
    Salt_Peter
    Dec 25, 2006
  6. Ed
    Replies:
    1
    Views:
    469
  7. ag
    Replies:
    2
    Views:
    548
    red floyd
    Feb 12, 2009
  8. Noah Roberts
    Replies:
    6
    Views:
    1,581
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...