我有几个带有模板化成员函数的类和一个预定义的类型列表,它们将与它们一起使用(Wandbox 链接:
// main.cpp:
#include "class.h"
int main(int argc, char** argv) {
A a;
a.foo(5);
a.foo(5.);
B b;
//b.bar(1,2,3,4); b.bar(1,2,3.,4);
return 0;
}
// class.h
#pragma once
struct A {
template<typename T> void foo(T x);
};
struct B {
template<typename T> void bar(int p1, int p2, T x, int p3);
};
// class.cpp
#include <iostream>
#include "class.h"
template<typename T> void A::foo(T x) {
std::cout << x << std::endl;
}
// explicit, but very verbose
// template void A::foo(int);
// ...
template<typename T> void ignore(T fn) {/* Use fn? */}
template<class Class>
void instantiate(Class) {
// List all types the function should be instantiated for here
ignore(&Class::template foo<int>);
ignore(&Class::template foo<double>);
}
// works, instantiates A::foo<int> and A::foo<double>
template void instantiate(A);
// How to pass B::foo and additional parameters?
// template void instantiate(B);
键入要实例化的成员函数和类型的每个组合,但它有几个缺点:
- 这很乏味
- 必须输入每个参数的整个函数签名,并且
- 必须在几个地方进行功能签名更改
- 必须为每个成员函数添加一个类型到列表中
我上面的解决方法适用于我测试过的大多数旧编译器(C++03 兼容性将是一个巨大的优势),但我不确定是否允许智能编译器删除未使用的参数和函数实例化。
对于常规函数,有一些解决方法,但据我了解,它们不适用于成员函数。
如何更改我的instantiate
函数以接受成员函数和附加参数?