私は次のようなテンプレート関数を持っています:
using namespace std::chrono;
using namespace std::chrono_literals;
template <typename D>
time_point<system_clock, D> makeTime(
int year, int month, int day, int hour = 0, int minute = 0,
int second = 0, int ms = 0, int us = 0, int ns = 0 );
通常、私はそれを次のように呼びます: auto us_tp1 = makeTime<microseconds>( 2020, 5, 26, 21, 21, 21, 999, 123 );
しかし今、私は次のようなエイリアス「makeTimeUS」を介してどこかでそれを呼び出す必要があります。
auto us_tp1 = makeTimeUS( 2020, 5, 26, 21, 21, 21, 999, 123 );
そのように、makeTimeUSはmakeTimeのインスタンスです。
私はこれを試しました:
using makeTimeUS = template time_point<system_clock, microseconds> makeTime;
この:
using makeTimeUS = template time_point<system_clock, microseconds> makeTime(
int, int, int, int, int, int, int, int, int );
しかし、どちらもコンパイルに合格することはできません。
テンプレート関数をインスタンス化し、同時にエイリアスを与えるにはどうすればよいですか?そうする必要がある理由は、makeTimeUSを呼び出す古いコードが多すぎて、テンプレートではなく通常の関数であるかのようになっているためです。ありがとう!
必要な関数への関数ポインタを取得し、それを「エイリアス」として使用できます。それは次のようになります:
auto makeTimeUS = makeTime<microseconds>;
そして、次のように使用できます:
auto us_tp1 = makeTimeUS( 2020, 5, 26, 21, 21, 21, 999, 123 );
ただし、これで名前を変更できます。これは関数ポインタであるため、デフォルトの引数は機能しなくなり、すべてのパラメータを指定する必要があります。
これを回避するには、ラムダを使用してエイリアスの代わりにラッパーを作成できます。これは次のようになります。
auto makeTimeUS = [](int year, int month, int day, int hour = 0,
int minute = 0, int second = 0, int ms = 0)
{
return makeTime<microseconds>(year, month, day, hour, minute, second, ms);
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加