Can you compile the following template function code?

This is a discussion on Can you compile the following template function code? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Here is the code and related compile error information. I am using Visual Studio 2008. Not sure whether ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Can you compile the following template function code?

    Hello everyone,


    Here is the code and related compile error information. I am using Visual Studio 2008. Not sure whether you can compile using your compiler? Why Visual Studio 2008 can not deduce template parameter type?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T> void f(int a) {g (a);}
    
    void g(int a)
    {
    	cout << a << endl;
    }
    
    int main()
    {
    	f (100); // can not compile, error C2783: 'void f(int)' : could not deduce template argument for 'T'
    	// f<int> (100) can compile
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,971
    Tested with the MinGW port of g++ 3.4.5
    Compiling: main.cpp
    main.cpp: In function `void f(int)':
    main.cpp:5: error: there are no arguments to `g' that depend on a template parameter, so a declaration of `g' must be available
    main.cpp:5: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
    main.cpp: In function `int main()':
    main.cpp:14: error: no matching function for call to `f(int)'
    3 errors, 0 warnings
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    How would it? It can only deduce template parameters that show up in the argument list, but you stealthily replaced T by int in the definition.

    Actually, because g(a) is now no longer dependent, it shouldn't compile with an explicit parameter ...
    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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,971
    In any case, it seems rather pointless to have a function template where the parameter is not used, so even if f<int>(100) is allowed, it is still pointless.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    Seems Bjarne is wrong? :-)

    The code is (with small adaption) from his book, section C.13.8.3 Point of Instantiation Binding.

    Quote Originally Posted by CornedBee View Post
    How would it? It can only deduce template parameters that show up in the argument list, but you stealthily replaced T by int in the definition.

    Actually, because g(a) is now no longer dependent, it shouldn't compile with an explicit parameter ...

    Hi laserlight,


    Seems f<int>(100) is the only way to call it. Why do you say the parameter is not used? It is used to pass to function g inside function f. Any comments?

    Quote Originally Posted by laserlight View Post
    In any case, it seems rather pointless to have a function template where the parameter is not used, so even if f<int>(100) is allowed, it is still pointless.


    regards,
    George

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    You are never using the type T anywhere in your template code. You specify a template with type T, but the only argument you have is int. Type T is never used anywhere.
    The compiler cannot deduce the type for T since you never specify any argument for it.
    The type T is unused. The only argument is int, which you pass in 100.
    So the compiler has no idea whatsoever what type T is. And again, it's never used anywhere in the template code so it's redundant (does not need to be there), which is what everyone is telling you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Question answered.

    Quote Originally Posted by Elysia View Post
    You are never using the type T anywhere in your template code. You specify a template with type T, but the only argument you have is int. Type T is never used anywhere.
    The compiler cannot deduce the type for T since you never specify any argument for it.
    The type T is unused. The only argument is int, which you pass in 100.
    So the compiler has no idea whatsoever what type T is. And again, it's never used anywhere in the template code so it's redundant (does not need to be there), which is what everyone is telling you.

    regards,
    George

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The code is (with small adaption) from his book, section C.13.8.3 Point of Instantiation Binding.
    Guess what part is to blame for the compile error.
    Yep, that's right, the small adaption. You replaced the T parameter with an int parameter.
    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
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    The compile error and the "small" adaption gives me a good lesson.

    Quote Originally Posted by CornedBee View Post
    Guess what part is to blame for the compile error.
    Yep, that's right, the small adaption. You replaced the T parameter with an int parameter.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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