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. Advertising

  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. Advertising

  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. 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. Fernando Cuenca
    Replies:
    4
    Views:
    2,559
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    661
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    1
    Views:
    617
    Salt_Peter
    Dec 25, 2006
  4. Ed
    Replies:
    1
    Views:
    359
  5. ag
    Replies:
    2
    Views:
    478
    red floyd
    Feb 12, 2009
Loading...

Share This Page