1

I am trying to store a bunch of templated function pointer in an array to function pointer.

i tried

template<typename T> 
void (*funcptrArray[100])(typename vector<T> &a,int,int,bool (*comp)(T,T));

it gives compilation error in g++

** Error expected primary expression before template**

EDIT: What i am trying to achieve here is: Given 10 different algorithms (templated function), for same task.I Want to store different instantiations in array so that I can iterate and run them one by one in a loop

8
  • @Kerrek SB No typedef here Commented Sep 5, 2013 at 22:23
  • As it stands, your question makes no sense. A "function pointer" is an object, and objects have types. A template is not a type (it's a template.) You can't have a "templated object". Commented Sep 5, 2013 at 22:23
  • Are you trying to create an array of function pointers that can refer to different template instantiations? Commented Sep 5, 2013 at 22:24
  • @greatWolf Yes this is what i exactly want to do Commented Sep 5, 2013 at 22:25
  • 2
    Can you edit your post to describe a representative, minimal, realistic situation? I think we're stabbing in the dark a little too much here. Commented Sep 5, 2013 at 22:37

1 Answer 1

1

You can only create class or function templates. There is no such thing as a variable templates, yet (there was talk about doing something like that, though). One work-around is to use a class template providing access to the array instead:

template <typename T>
struct func_ptr {
    static void (*array[100])(std::vector<T>&, int, int, bool (*)(T, T);
};

template <typename T>
void (*func_ptr<T>::array[100])(std::vector<T>&, int, int, bool (*)(T, T);

This should work but isn't too scalable. Wrapping the array into a function template and returning a reference to the array will work better: This way it is easy to get just one instance of the array as static variable in function templates need to be merged between different instantiations with the same parameters. Writing out the return type of the function template is just a bit awkward:

template <typename T>
void (*(&funcPtrArray())[100])(std::vector<T>&, int, int, bool (*)(T, T))
{
    static void (*array[100])(std::vector<T>&, int, int, bool (*)(T, T));
    return array;
}

I was wondering if C++11 helps but it doesn't:

auto funcPtrArray() -> void (*(&)[100])(std::vector<T>&, int, int, bool (*)(T, T))
{
    ...
}

You would then use the function to get you the array, e.g.:

std::vector<int> vec;
funcPtrArray<int>()[12](vec, 1, 1, [](int a, int b){ return a < b; });
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks that helps. I still don't understand if you can have the array of function template wrapped inside a function why can't you have it without a wrapper?
@David: Conceptually, it would work but currently you only templatize functions and classes. As I said, there was talk about creating variable templates. I could also imagine enum class templates (e.g., to set the values of certain members) and namespace templates (everything within is templatized on the namespace's type). However, none of these things are currently allowed.
@David note this is not an array of function templates in the sense you are looking to use like in your earlier comment. This creates an array of func ptrs to one particular instance of T. For example, you create 100 func ptrs that can only refer to functions taking a vector of ints. You cannot for example, assign a function taking vector<foobar> as parameter and store it into this array.
@Dietmar, if i have to assign two different function to funcPtrArray how can i do that? imagine i have qsort(vector<int> v, int start,int end,bool (*comp)(int,int)), and mergesort(vector<int> v, int start,int end,bool (*comp)(int,int)) which you want to store in the array pointer
@David: Well, you'd assign to the array elements instead of calling them: funcPtrArray<int>()[0] = qsort; funcPtrArray<int>()[1] = mergesort;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.