Real answer: No. If you want different types, write different types. The entire concept is insane.
Toy answer, sure. For one, you could macro out the type:
template <int N>
struct object {
object(int ) { }
};
#define object object<__LINE__>
Or, since we're being cheeky, you could macro out both names foo
and bar
:
template <int N>
struct object {
object(int ) { }
};
#define foo(x) <0> foo(x)
#define bar(x) <1> bar(x)
Or, maybe just one of them:
template <class T>
struct object {
object(T ) { }
};
#define foo(x) <double> foo(x)
Or, you could use stateful metaprogramming to hack into different values. This works on gcc but not on clang:
template <int N>
struct flag
{
friend constexpr int adl_flag(flag<N>);
constexpr operator int() { return N; }
};
template <int N>
struct write
{
friend constexpr int adl_flag(flag<N>) { return N; }
static constexpr int value = N;
};
template <int N, int = adl_flag(flag<N>{})>
constexpr int read(int, flag<N>, int R = read(0, flag<N + 1>{}))
{
return R;
}
template <int N>
constexpr int read(float, flag<N>)
{
return N;
}
template <class T, int N=0, int R=write<read(0, flag<0>{})+N>::value>
struct object {
object(T ) { }
};