Skip to content

Instantly share code, notes, and snippets.

@Cryolite
Created March 10, 2012 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cryolite/2010777 to your computer and use it in GitHub Desktop.
Save Cryolite/2010777 to your computer and use it in GitHub Desktop.
#include <type_traits>
void f(std::integral_constant<int, 0>);
template<int I>
auto f(std::integral_constant<int, I>)
-> decltype(f(std::integral_constant<int, I - 1>()));
int main()
{
static_assert(
std::is_same<
decltype(f(std::integral_constant<int, 2>())), void>::value, "");
// このコードは,上記の static_assert 中の f の呼び出しが
//
// - オーバーロード解決の結果 f<2>(std::integral_constant<int, 2>) に合致する
// - この戻り値型は decltype(f(std::integral_constant<int, 1>())) である
// - この戻り値型を決定するため,次に f(std::integral_constant<int, 1>()) に対する
// オーバーロード解決が開始される
// - その結果, f<1>(std::integral_constant<int, 1>) に合致する
// - この戻り値型は decltype(f(std::integral_constant<int, 0>())) である
// - この戻り値型を決定するため,次に f(std::integral_constant<int, 0>()) に対する
// オーバーロード解決が開始される
// - その結果,非テンプレート関数 f(std::integral_constant<int, 0>) と
// テンプレートのインスタンス化の結果である f<0>(std::integral_constant<int, 0>) の
// 2つが候補に挙がる
// - オーバーロード解決の規則に基づいて,非テンプレート関数が優先して合致する
// - 結果, f(std::integral_constant<int, 0>()) に対して,非テンプレート関数
// f(std::integral_constant<int, 0>) が合致する
// - f(std::integral_constant<int, 0>) の戻り値型は void なので最終的に上の
// static_assert 中の f の呼び出しの戻り値型も void と決定される.
// - したがって,上記の static_assert は well-formed となる
//
// ……という意図のつもりである.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment