存在するかどうかわからない構文糖衣を探しています。説明するのが難しいので、例を示します。
#include <iostream>
using namespace std; // haters gonna hate
union MyUnion
{
float f;
unsigned int i;
// more types
};
unsigned int someFunction(MyUnion m)
{
return m.i;
}
int main()
{
unsigned int i = 10;
float f = 10.0;
// this is the syntactic sugar I'm looking to emulate
cout << someFunction(i) << endl; // automagically initializes unsigned int member
cout << someFunction(f) << endl; // automagically initializes float member
return 0;
}
関数のオーバーロードの束を定義し、スタックで共用体を宣言し、次のように初期化できることを認識しています。
unsigned int someFunction(unsigned int i)
{
return i; // this is the shortcut case
}
unsigned int someFunction(float f)
{
MyUnion m = {f};
return m.i;
}
// more overloads for more types
しかし、もっと良い方法があることを願っています。ある?