Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. simple template question

simple template question

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studio
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    Hi, I typed in the following code and found it to compile and run ok (using VS 2003). #include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; } I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks!

    C S 2 Replies Last reply
    0
    • M minkowski

      Hi, I typed in the following code and found it to compile and run ok (using VS 2003). #include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; } I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks!

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      minkowski wrote:

      template void swapa(T & a, U & b)

      Isn't it

      template void <class T, class U> void swapa(T & a, U & b)

      ?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • M minkowski

        Hi, I typed in the following code and found it to compile and run ok (using VS 2003). #include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; } I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks!

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        It's a non-compliance in VS2003. Both gcc4.0.1 and VS2008 raise an error on that line: gcc4.0.1 =>

        Desktop$ g++ -o a a.cpp
        a.cpp: In function ‘int main()’:
        a.cpp:16: error: ISO C++ forbids declaration of ‘type name’ with no type

        VS2008 =>

        cl -EHsc -O2 a.cpp
        Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
        Copyright (C) Microsoft Corporation. All rights reserved.

        a.cpp
        a.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups