Skip to content

Instantly share code, notes, and snippets.

@hisui
Created December 28, 2012 11:05
Show Gist options
  • Save hisui/4396877 to your computer and use it in GitHub Desktop.
Save hisui/4396877 to your computer and use it in GitHub Desktop.
constexprはランタイム時にも呼び出しできるから、引数の値をテンプレート引数に渡すことは出来ない件(´;ω;`)
#include <type_traits>
#include <iostream>
extern void *enabler;
// 文字のリスト
template<char H, typename T>
struct cons {};
struct nil {};
template<size_t N, size_t I = 0
, typename std::enable_if<I == N>::type *& = enabler>
constexpr auto make_str(char const(&a)[N]) -> nil;
template<size_t N, size_t I = 0
, typename std::enable_if<I != N>::type *& = enabler>
constexpr auto make_str(char const(&a)[N])
-> cons<a[I],decltype(make_str<N,I+1>(a))>; // これはNG...
int main()
{
typedef decltype(make_str("unko")) str; // これがやりたかったのだけど
auto &info = typeid(str);
std::cerr << info.name() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment