4
votes

I have a variadic template member function defined as:

template<typename ... Params>
 VAlgorithm* CreateAlgorithm(const char *objectName, const char *className, Params ... par)

and I would like to take the address of the specialized version where Params contains no type (which I call the "empty" specialization), ie., of:

VAlgorithm* CreateAlgorithm(const char *objectName, const char *className)

I tried in several ways. The naive way:

&AlgorithmFactory::CreateAlgorithm<>

(since, for example, &AlgorithmFactory::CreateAlgorithm< int > works) and a more explicit way:

(VAlgorithm* (*)(const char*, const char*))AlgorithmFactory::CreateAlgorithm<>

With the explicit way, GCC 4.7.1 says:

error: insufficient contextual information to determine type

It seems that the "empty" specialization is not understood by the compiler, which interprets the missing template types as a lack of info and not as "no types" information. What is the correct way to do this? (Sorry for the potentially naive question but I'm fairly new to variadic templates and I found no documentation on this topic). Thanks

1
Could you post more of your code, e.g. the definition of AlgorithmFactory and the context where you're taking the address? - ecatmur
AlgorithmFactory is a class whith no parents; Createalgorithm is defined as: template<typename ... Params> VAlgorithm* CreateAlgorithm(const char *objectName, const char *className, Params ... par). I'm trying to take the address from inside the main function. - Nicola Mori
You have declared CreateAlgorithm static, right? - ecatmur
No, I didn't. I can do it since AlgorithmFactory is meant to be a singleton. But why it works without static if I specify at least one type and conversely it requires static if I leave the types list blank? - Nicola Mori
That's undefined behaviour; you can't convert a pointer to member function to a function pointer (a static member function is not a member function). - ecatmur

1 Answers

1
votes

Your code should work; see e.g. http://liveworkspace.org/code/6253cf45f416be60879b93aa74c24de8

All the following syntaxes work for me:

struct S {
  template<typename... Args> static int *foo(const char *, const char *, Args...);
};

int main() {
    (int *(*)(const char *, const char *))S::foo<>;
    (int *(*)(const char *, const char *))S::foo;
    (int *(&)(const char *, const char *))S::foo<>;
    (int *(&)(const char *, const char *))S::foo;
    int *(&f)(const char *, const char *) = S::foo<>;
    int *(&g)(const char *, const char *) = S::foo;
    int *(*h)(const char *, const char *) = S::foo<>;
    int *(*i)(const char *, const char *) = S::foo;
}