Thread: Function Pointers to Templated Functions

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Function Pointers to Templated Functions

    What I want to do is to store the function address of a templated function. An idea of what I want to do:

    Code:
    class SMC
    {
    public:
    	template <class type> printy(int a)
    	{
    		for (type i=0; i<10; i++)
    			cout << a;
    	}
    
    	Special_MethodCall()
    	{
    		void (SMC::*boo)(int)=&SMC::printy<int>;
    	}
    };
    You see, what I want to do is to make a encoding function that can encode various types of array data. Therefore --> templates. I want to put these all in a class and get function pointers to specific templates of them, like ex: printy<float>.

    However, when I try to compile this under MS C++, it gives me a fatal compiler error. Not good, how would you do this?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Eh, which version of MSVC? As far as I know, 12.00 (6) doesn't support templated member functions.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I'm using C++.NET Unmanged (normal, native code). It does allow templated member functions but still it doesn't matter. If you want, pretend the class wasn't there and that they were global functions. You still can't get the address of a specific templated function. Help!

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Eh, which version of MSVC? As far as I know, 12.00 (6) doesn't support templated member functions.
    It supports them, but with a few problems:

    The templatization MUST be implicity on templated member function is MSVC++ 6 (the parameterization must be able to be implied by the member function parameters).

    Also, in MSVC++ 6. the definition must be inline with the templated definition in class definition.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Is there something wrong with my function pointer syntax? Do I have to add a keyword or something? Or is it just not possible? I would hate to have C++ limited like that because writing out my functions would be really gay.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    &SMC::Printy<int>

    Printy<int> will cause a problem in MSVC++ 6.0 (it's a problem with the compiler). It can only work with templated member functions when the parameterization is implicit with the function parameters. This means you can't work with pointers to them in MSVC++ 6.0. (least I don't think so, i've never tried, I've only experienced this phenomenon when calling the function).

    Also, you forgot to specifiy the return type when you defined the member function!

    In this particular case i don't see WHY you're using templates anyways. Your function is not really a candidate for templating.

    EDIT: the function doesn't really seem to make much sense period. all you're doing is outputting the value of a 10 times. The only time that might change is if you overloaded operators for a class and called that version of the function. Unless you're catering to that situation (which seems highly unlikely), then templating is pointless here.
    Last edited by Polymorphic OOP; 02-12-2003 at 08:03 AM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Yeah, I forgot the return type! Remember, this is only a sample. Hell if I write a templated function like that for real. Its pointless. I don't want to release the real code, but the concept is there, I want a function ptr.

    That sux that I can't. I guess its time to write them all out, all 42 of them.... time to use includes, defines, oh man, a whole truck load of them!

    EDIT: There's a lot of errors there!
    Last edited by Speedy5; 02-12-2003 at 08:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM