Templates react badly to local classes?

This is a discussion on Templates react badly to local classes? within the C++ Programming forums, part of the General Programming Boards category; The following program, which uses a local class object as an argument to a templated function, does not compile using ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    835

    Templates react badly to local classes?

    The following program, which uses a local class object as an argument to a templated function, does not compile using gcc 4.1.1:

    Code:
    template<class T>
    void f(T) {}
    
    int main() {
      class A {} a;
      f(a);
    }
    The error is:

    test.cxx: In function ‘int main()’:
    test.cxx:6: error: no matching function for call to ‘f(main()::A&)’

    However, if I make the class nonlocal, then it does:

    Code:
    template<class T>
    void f(T) {}
    
    class A {};
    
    int main() {
      A a;
      f(a);
    }
    I'm new to C++, and suspect that using the local class is somehow illegal. Is this true, or is the compiler broken?
    Last edited by robatino; 09-20-2006 at 02:24 PM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Dont takes this to well, but looking at your second code snippet, you delcarted the function, and the compiler said it was ok. As a general rule of thumb in C++, every function ( main being the only exception to this ) must have a prototype to tell the compiler what type the function is example 'int' and any parameters.

    If you do not supply a prototype, some pr most compilers complain with an error or a warning.

    >is the compiler broken?

    Lol, I doubt it, as many compilers would produce the same error

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I don't understand what you're talking about - both f and A are defined (and hence prototyped) in each snippet. The only difference is that in the first snippet, the definition of A is local to main(). Since I only need access to A inside main(), why is this illegal?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    I don't know this for sure, but it doesn't compile in VC++ 2003 and gives an error with the following help:
    You cannot instantiate a function based on a function template based on a local type. Types used to instantiate must have external linkage.
    So my guess is that gcc is right. It may also be that modern compilers haven't caught up with the standard on this, though.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Well, in order for this to be properly defined,

    Code:
    template<class T>
    void f(T) {}
    T must not be local.

    The error message may seem a little cryptic. But if you pay close attention to it, you will notice that it is not looking for 'a'. The object name is not worrying it. It's a reference to main::A that it is looking.

    As such, the function instantiating is failing because the template parameter is local. At the point of definition, the function cannot find main::A
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I found the following thread which confirms that what I was trying to do is illegal.

    http://www.velocityreviews.com/forum...ocal-type.html

    The PDF document mentioned at the end is interesting. As I understand it, this is something that ought to be allowed, but the language currently doesn't (presumably because it would be hard to implement).

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    > As I understand it, this is something that ought to be allowed, but the language currently doesn't (presumably because it would be hard to implement).

    Something like that, probably yes. Because the following is allowed:

    Code:
    template<class T>
    void f(T) {}
    
    int main() {
      int a = 12;
      f(a);
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But then, int is not a local type.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    > But then, int is not a local type.

    doh!
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You can't pass a local type to any other function, templated or not, IIRC. I almost never use local classes.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  2. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. VC++6: exporting classes containing templates
    By ttt in forum Windows Programming
    Replies: 2
    Last Post: 09-15-2003, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21