#explicit instantiation of template function with forwarding

9 messages · Page 1 of 1 (latest)

rare violet
#
#include <string>

class FOO{
public:
    FOO(int a,char b,std::string c){};
};

template<typename T>
T* getNewImpl(int a,char b,std::string c){
    return new T(a,b,c);
}

template<typename T,typename... Args>
T* getNew(Args&&... args){
    return getNewImpl<T>(std::forward<Args>(args)...);
}

extern template FOO* getNew<FOO> (int,char,std::string); 

/*
compile error with
test.cpp:18:22: error: explicit instantiation of 'getNew' does not refer to a function template, variable template, member function, member class, or static data member
extern template FOO* getNew<FOO> (int,char,std::string);
                     ^
test.cpp:14:4: note: candidate template ignored: could not match 'type-parameter-0-1 &&' against 'int'
T* getNew(Args&&... args){
   ^
1 error generated. 
*/

int main(){
    getNew<FOO>(1,'a',"hello"); /* works */
    return 0;
}
past oxideBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

frank bluff
#

;compile

#include <string>

class FOO{
public:
    FOO(int a,char b,std::string c){};
};

template<typename T>
T* getNewImpl(int a,char b,std::string c){
    return new T(a,b,c);
}

template<typename T,typename... Args>
T* getNew(Args&&... args){
    return getNewImpl<T>(std::forward<Args>(args)...);
}

extern template FOO* getNew<FOO, int, char, std::string>(int&&,char&&,std::string&&); 

int main(){
    getNew<FOO>(1,'a',"hello"); /* works */
    return 0;
}
snow dustBOT
#
Compilation successful

No output.

hallow oar
#

With the template argument Args... = int, char, std::string, the expected parameters Args&&... should be int&&, char&&, std::string&&.

rare violet
#

Sincerely thanks. That's really insane... It works(at least no compile error therr). Thank u all.

#

!solved