Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Created April 25, 2017 05:41
Show Gist options
  • Save jtbandes/1b242290968a44ef74f6af55ca547f90 to your computer and use it in GitHub Desktop.
Save jtbandes/1b242290968a44ef74f6af55ca547f90 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
template<int N, char C, char... Cs> struct repeatImpl : repeatImpl<N-1, C, C, Cs...> {};
template<char C, char... Cs> struct repeatImpl<0, C, Cs...> {
static constexpr char cs[] = {Cs..., '\0'};
};
template<char C, char... Cs> constexpr char repeatImpl<0, C, Cs...>::cs[];
template<int N, char C> const char (& repeat())[N+1] {
return repeatImpl<N, C>::cs;
}
int main (int argc, char const *argv[])
{
cout << "str: " << repeat<5, 'x'>() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment