#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;
}
#explicit instantiation of template function with forwarding
9 messages · Page 1 of 1 (latest)
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.
;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;
}
Compilation successful
No output.
saqib.com | 49ms | c++ | x86-64 gcc 14.2 | godbolt.org
With the template argument Args... = int, char, std::string, the expected parameters Args&&... should be int&&, char&&, std::string&&.
Crazy
Just realized