I wrote a simple type-based dispatcher using an unnamed parameter, which I assume is pretty normal.
When it came to calling the function, I wanted to get the overload without having any variable. Is it possible?
eg.
void f1(int ) {
}
int main(int, char*) {
f1();
return 0;
}
My “real” example was a simple type-based dispatcher. The best I came up with was a pointer as an unnamed parameter, I think it has the least overhead:
#include <iostream>
using namespace std;
template<typename U>
class A {
public:
A(const U& u) : u(u) {};
template <typename T>
T get_as() {
T* t;
return get(t);
}
U get(U*) {
return u;
}
int get(int*) {
return i;
}
double get(double*) {
return d;
}
private:
U u;
int i = 5;
double d = 3.14;
};
int main() {
A<string> a("name");
cout << a.get_as<double>() << '\n';
cout << a.get_as<int>() << '\n';
cout << a.get_as<string>() << '\n';
return 0;
}
I assume this is purely aesthetically pleasing with a pointer, because it is probably optimized, but still I am interested. I also suggest that this may not be possible because (AFAIK) this argument goes through the stack. So technically there is always “overhead”. Is it correct?
, , " " ?
PS. , T*(0) "" , , , .