Thread: Template function pointer???

  1. #1
    Registered User NorthBlast's Avatar
    Join Date
    Feb 2014
    Location
    Toronto, ON
    Posts
    3

    Question Template function pointer???

    I am learning c++ and in the past two week, I started learning and practicing about Function Pointers and Templates.. but it came to my mind if I can have a template function pointer..
    So, here is what I have coded as I started my practice to try to understand whats going on... but Visual Studio 2013 is not accepting my function call in the main method... not sure what I am doing wrong and its saying that my function pointer is undefined but I thought it was?

    Code:
    #include <iostream>
    
    template<class T>
    int foo(T& x){
        return x * 500;
    }
    
    template<class T>
    T (*myFunc)(T) = foo;
    
    int main(){
    
        std::cout << myFunc(7) << std::endl;
    
        system("pause");
        return 0;
    }
    Well, I really hope you guys can help me to understand if what I want to do is possible and which is the proper way and why?
    Thanks a lot.. if required more details my questions please let me know!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even if it were to work, you've got some mismatches (for instance, myFunc<int> (if it were to exist) would accept an int parameter, but foo<int> accepts an int& parameter).

    I suspect that function pointers can't be templated, but I'm not in front of my standard to check. The following does work:
    Code:
    #include <iostream>
    
    template <class T>
    int foo(T x) {
        return x * 500;
    }
    
    int (*myFunc)(int) = foo<int>;
    
    int main() {
    
        std::cout << myFunc(7) << std::endl;
        return 0;
    }
    Note I made foo accept a T instead of a T& (otherwise you can't pass a const literal like "7" to it), and the function pointer is of a definite type.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not possible to create a function pointer to a template function because the template function doesn't "exist" yet, so where would the pointer point to?
    The compiler works by stamping out a specific function from the template as it is instantiated. When it is instantiated, it starts to exist, so it doesn't make sense to have a function pointer to a template function.
    But if there's a specific problem you're trying to solve, you should mention that instead. Then we can probably suggest an alternate way of achieving what you want to do.
    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.

  4. #4
    Registered User NorthBlast's Avatar
    Join Date
    Feb 2014
    Location
    Toronto, ON
    Posts
    3

    Smile Thanks!

    Quote Originally Posted by tabstop View Post
    Even if it were to work, you've got some mismatches (for instance, myFunc<int> (if it were to exist) would accept an int parameter, but foo<int> accepts an int& parameter).

    I suspect that function pointers can't be templated, but I'm not in front of my standard to check. The following does work:
    Code:
    #include <iostream>
    
    template <class T>
    int foo(T x) {
        return x * 500;
    }
    
    int (*myFunc)(int) = foo<int>;
    
    int main() {
    
        std::cout << myFunc(7) << std::endl;
        return 0;
    }
    Note I made foo accept a T instead of a T& (otherwise you can't pass a const literal like "7" to it), and the function pointer is of a definite type.
    Thanks for the help, I was actually working within the program to also try the answer you gave me.. I had the mismatch of T& in step of T..

  5. #5
    Registered User NorthBlast's Avatar
    Join Date
    Feb 2014
    Location
    Toronto, ON
    Posts
    3
    Thanks, no.. It was just a question that came up to my head and I was looking for answer.. I am just practicing some new topics learned and just thought if I could do something like that..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-13-2013, 04:13 PM
  2. Pointer to template function workaround
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 01-13-2012, 04:18 PM
  3. Replies: 3
    Last Post: 01-30-2011, 04:28 PM
  4. template parameter on a function that dont use the template
    By underthesun in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 05:38 PM
  5. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM

Tags for this Thread