Error: template argument uses local type

Discussion in 'C++' started by Gabriel Schreiber, Nov 27, 2003.

  1. Hi

    this code:

    #include <vector>
    void f()
    {
    struct arg{};
    std::vector<arg> v;
    };

    gets the error message:
    : template-argument `f()::arg' uses local type `f()::arg'
    : template argument 2 is invalid

    can anyone explain this?

    And: Is it possible in any way to instantiate templates with local
    types?

    TIA
    Gabriel
     
    Gabriel Schreiber, Nov 27, 2003
    #1
    1. Advertising

  2. Gabriel Schreiber

    Rolf Magnus Guest

    Gabriel Schreiber wrote:

    > Hi
    >
    > this code:
    >
    > #include <vector>
    > void f()
    > {
    > struct arg{};
    > std::vector<arg> v;
    > };
    >
    > gets the error message:
    > : template-argument `f()::arg' uses local type `f()::arg'
    > : template argument 2 is invalid
    >
    > can anyone explain this?


    The compiler already said it. You're using a local type as template
    argument, and that's not allowed in c++. Put the struct definition
    ouside the function, and it should work.

    > And: Is it possible in any way to instantiate templates with local
    > types?


    No.
     
    Rolf Magnus, Nov 27, 2003
    #2
    1. Advertising

  3. Gabriel Schreiber

    tom_usenet Guest

    On Thu, 27 Nov 2003 12:35:34 +0100, Gabriel Schreiber
    <-aachen.de> wrote:

    >Hi
    >
    >this code:
    >
    > #include <vector>
    > void f()
    > {
    > struct arg{};
    > std::vector<arg> v;
    > };
    >
    >gets the error message:
    >: template-argument `f()::arg' uses local type `f()::arg'
    >: template argument 2 is invalid
    >
    >can anyone explain this?
    >
    >And: Is it possible in any way to instantiate templates with local
    >types?


    No, the problem is that template arguments must have external linkage,
    but local types have no linkage. You can move the struct to an
    anonymous namespace to work around the problem without polluting a
    namespace with your type.

    You might want to read this:
    http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1427.pdf

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Nov 27, 2003
    #3
    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. IR
    Replies:
    3
    Views:
    421
  2. Filip Konvicka

    local type as template argument

    Filip Konvicka, May 21, 2007, in forum: C++
    Replies:
    2
    Views:
    450
  3. nw
    Replies:
    0
    Views:
    316
  4. nguillot
    Replies:
    5
    Views:
    532
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,261
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page