
22
|
1 章 型推論
template<typename Container, typename Index> // final
decltype(auto) // C++14
authAndAccess(Container&& c, Index i) // version
{
C++14 の最終
バージョン
authenticateUser();
return std::forward<Container>(c)[i];
}
上例は期待した内容をすべて実現してくれますが、C++14 コンパイラが必要です。読者が C++14
コンパイラを持っていなければ、C++11 バージョンのテンプレートを使用せざるを得ません。C++14
バージョンと基本的には同じですが、戻り型を明示する必要があります。
template<typename Container, typename Index> // final
auto // C++11
authAndAccess(Container&& c, Index i) // version
-> decltype(std::forward<Container>(c)[i])
C++11 の最終
バージョン
{
authenticateUser();
return std::forward<Container>(c)[i];
}
読者を悩ませる点がもう 1 つあるとすれば、本項目の冒頭で著者が decltype はほぼ常に期待通
りの結果となる、すなわちごくまれに予想外の結果となると述べたこ