[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

How far can you push c++11 templates?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 2

File: oi0ec0.jpg (28KB, 512x279px) Image search: [Google]
oi0ec0.jpg
28KB, 512x279px
I just mad the basic star triangle program with only templates. Show me what you got!
#define TRIANGLE_SIZE 20

#include <iostream>

template<int x> struct fact{
enum{ value = x + 1 + fact<x - 1>::value};
};

template<> struct fact<0>{
enum{ value = 1 };
};


template<char... C> struct charlist{};

template<char left, typename charlist> struct toFront;
template<char left, char... right> struct toFront<left, charlist<right...> >{
typedef charlist<left, right...> value;
};

template<typename charlist, typename charlistx> struct combine;
template<char... x, char... y> struct combine<charlist<x...>, charlist<y...> >{
typedef charlist<x... , y...> value;
};


template<int length> struct makeRow{
typedef typename toFront<'*', typename makeRow<length-1>::value >::value value;
};

template<> struct makeRow<0>{
typedef charlist<'\n'> value;
};

template<int size> struct makeTri{
typedef typename combine< typename makeTri<size-1>::value, typename makeRow<size>::value >::value value;
};

template<> struct makeTri<0>{
typedef charlist<> value;
};

template<typename charlist> class consttri;
template<char... c> class consttri< charlist<c...> >{
public:
char value[sizeof...(c)+1];
constexpr consttri(): value{c..., '\0'}{}
};

void printstars(int x){
int y = 0;
while(y <= x){
for(int m=0; m<y; m++){
std::cout<<"*";
}
std::cout<<std::endl;
y++;
}
}

int main(){
std::cout<<"Compile time: "<<std::endl;
std::cout<< consttri< typename makeTri<TRIANGLE_SIZE>::value >().value;
std::cout<< "Run time: "<<std::endl;
printstars(TRIANGLE_SIZE);
}
>>
>this much bullshit to generate a fucking triangle at compile time
>needing templates
import std.stdio;

string stars(int x)
{ char[] ret;
int y = 0;
while (y <= x)
{ for (int m = 0; m < y; ++m)
ret ~= "*";
ret ~= "\n";
++y;
}
return ret.idup;
}

void main()
{ pragma(msg, stars(12));
writeln(stars(12));
}
>>
>>61546464
>enum
>typedef
>constexpr constructor
You call that C++11 template metaprogramming? Absolutely disgusting.

#include <utility>
#include <iostream>

template<char...>
struct static_string;

template<typename...>
struct static_string_cat;

template<char... C, typename... A>
struct static_string_cat<static_string<C...>, A...>
{
using value = typename static_string_cat<
static_string<C...>, typename static_string_cat<A...>::value
>::value;
};

template<char... L, char... R>
struct static_string_cat<static_string<L...>, static_string<R...>>
{
using value = static_string<L..., R...>;
};

template<>
struct static_string_cat<>
{
using value = static_string<>;
};

template<typename>
struct static_string_cstr;

template<char... C>
struct static_string_cstr<static_string<C...>>
{
static constexpr char value[] = { C..., '\0' };
};

template<char... C>
constexpr char static_string_cstr<static_string<C...>>::value[];

template<std::size_t N, char C>
struct char_repeat
{
using value = typename static_string_cat<
static_string<C>, typename char_repeat<N - 1, C>::value
>::value;
};

template<char C>
struct char_repeat<1, C>
{
using value = static_string<C>;
};

template<unsigned N>
struct stars
: public static_string_cat<
typename char_repeat<N, '*'>::value, static_string<'\n'>
>
{
};

template<typename>
struct triangle_aux;

template<std::size_t... I>
struct triangle_aux<std::index_sequence<I...>>
{
using value = typename static_string_cat<
typename stars<I + 1>::value...
>::value;
};

template<std::size_t N>
struct triangle
{
using value = typename triangle_aux<std::make_index_sequence<N - 1>>::value;
};

int main(void)
{
static constexpr std::size_t TRIANGLE_SIZE = 20;
std::cout
<< static_string_cstr<typename triangle<TRIANGLE_SIZE>::value>::value;
}
>>
>>61547692
Please help I ran that and it formated my computer
>>
>>61547692
Impressive
>>
#include <iostream>

template<char... chars>
struct char_list
{
char value[sizeof...(chars)+1];
constexpr char_list(): value{chars..., '\0'}{}
};

template<size_t line, size_t tri_size, size_t row_size, char... collect>
struct _make_row
{ typedef typename _make_row<line, tri_size, row_size - 1, collect..., '*'>::type type; };

template<size_t line, size_t size, char... chars>
struct _make_tri
{
typedef typename _make_row<line, size, size - line + 1, chars...>::type type;
};

template<size_t line, size_t tri_size, char... collect>
struct _make_row<line, tri_size, 0, collect...>
{ typedef typename _make_tri<line - 1, tri_size, collect..., '\n'>::type type; };

template<size_t size, char... chars>
struct _make_tri<0, size, chars...>
{ typedef typename char_list<chars...> type; };


template<size_t size>
struct triangle
{ typedef typename _make_tri<size, size>::type type ; };

int main()
{
std::cout << triangle<10>::type().value << std::endl;
return 0;
}


pls rate
>>
>>61547110
BTFO
>>
>>61548033
Needs more templates.
>>
>>61548033
btw, don't know why but this code doesn't work with gcc, it runs fine on msvc 2017 tho so you can run it on this site if your interested. http://webcompiler.cloudapp.net/
>>
>>61548120
but it does everything at compile time and isn't cluttered
>>
>>61547692
we're on the verge of getting c++17 and you're still doing c++11? pathetic
>>
>>61548033
>using _ as a prefix for variable and type names
0/10 see me after class
>>
>>61548234
can I still get half credit professor anon?

#include <iostream>

template<char... chars>
struct char_list
{
char value[sizeof...(chars)+1];
constexpr char_list(): value{chars..., '\0'}{}
};

template<size_t line, size_t tri_size, size_t row_size, char... collect>
struct _make_row
{ using type = typename _make_row<line, tri_size, row_size - 1, collect..., '*'>::type; };

template<size_t line, size_t size, char... chars>
struct _make_tri
{ using type = typename _make_row<line, size, size - line + 1, chars...>::type; };

template<size_t line, size_t tri_size, char... collect>
struct _make_row<line, tri_size, 0, collect...>
{ using type = typename _make_tri<line - 1, tri_size, collect..., '\n'>::type; };

template<size_t size, char... chars>
struct _make_tri<0, size, chars...>
{ using type = typename char_list<chars...>; };


template<size_t size>
struct triangle
{ using type = typename _make_tri<size, size>::type; };

int main()
{
std::cout << triangle<10>::type().value << std::endl;
return 0;
}
>>
>>61548310
no
https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
>>
File: 1498774314426.jpg (146KB, 682x1082px) Image search: [Google]
1498774314426.jpg
146KB, 682x1082px
>>61546464
>this thread
OOP was a mistake.
>>
>outputs 1.5MB executable

Brilliant.
>>
How cant Rustfags even compete?
Thread posts: 17
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.