案ずるより産むが易しというもので、割と簡単に綺麗に書けた。こんな感じ。
char配列を可変長のテンプレート引数として結合していって、文字列定数を生成するというテクニックは実際に使い所があるかもと思った。最近C++書いてないけど。
#include <cstdio> template <typename LHS, int N> struct numstr { template <char... Args> struct append { typedef typename numstr<LHS, N / 10>::template append<'0' + N % 10, Args...>::result result; }; }; template <typename LHS> struct numstr<LHS, 0> { template <char... Args> struct append { typedef typename LHS::template append<Args...>::result result; }; }; template <int N, int Mod3 = N % 3, int Mod5 = N % 5> struct fizzbuzz { template <char... Args> struct append { typedef typename numstr<fizzbuzz<N - 1>, N>::template append<'\n', Args...>::result result; }; }; template <> struct fizzbuzz<0> { template <char... Args> struct append { struct result { const char data[sizeof...(Args)]; constexpr result() : data{Args...} {} }; }; }; template <int N> struct fizzbuzz<N, 0, 0> { template <char... Args> struct append { typedef typename fizzbuzz<N - 1>::template append<'F', 'i', 'z', 'z', 'B', 'u', 'z', 'z', '\n', Args...>::result result; }; }; template <int N, int Mod5> struct fizzbuzz<N, 0, Mod5> { template <char... Args> struct append { typedef typename fizzbuzz<N - 1>::template append<'F', 'i', 'z', 'z', '\n', Args...>::result result; }; }; template <int N, int Mod3> struct fizzbuzz<N, Mod3, 0> { template <char... Args> struct append { typedef typename fizzbuzz<N - 1>::template append<'B', 'u', 'z', 'z', '\n', Args...>::result result; }; }; int main() { constexpr fizzbuzz<100>::append<>::result s; fwrite(s.data, 1, sizeof(s.data), stdout); return 0; }コンパイルしてディスアセンブルすると、コンパイル時に生成された文字列定数をfwriteするだけのmain関数が生成されていることが確認できる。
$ clang++ -O2 -std=c++11 -pedantic -Wall fizzbuzz11.cc && otool -tV a.out a.out: (__TEXT,__text) section _main: 0000000100000dd0 pushq %rbp 0000000100000dd1 movq %rsp, %rbp 0000000100000dd4 movq 0x225(%rip), %rax ## literal pool symbol address: ___stdoutp 0000000100000ddb movq (%rax), %rcx 0000000100000dde leaq __ZZ4mainE1s(%rip), %rdi ## main::s 0000000100000de5 movl $0x1, %esi 0000000100000dea movl $0x19d, %edx 0000000100000def callq 0x100000df8 ## symbol stub for: _fwrite 0000000100000df4 xorl %eax, %eax 0000000100000df6 popq %rbp 0000000100000df7 retq
ちなみに、C++14だと、いたるところにconstexprがつけられるので、もっと普通のコードになる。
Georgia Football
ReplyDeleteGeorgia Football Game
Georgia College Football
Watch Georgia Football
Georgia Football Live
Georgia Football Live Stream
Michigan Football
Michigan Football Game
Michigan College Football
Watch Michigan Football
Michigan Football Live
Michigan Football Live Stream
Ohio State Football
Ohio State Football Game
Ohio State College Football
Watch Ohio State Football
Ohio State Football Live
Ohio State Football Live Stream
Nebraska Football
Nebraska Football Game
Nebraska College Football
Watch Nebraska Football
Nebraska Football Live
Nebraska Football Live Stream
LSU Football
LSU Football Game
LSU College Football
Watch LSU Football
LSU Football Live
LSU Football Live Stream
Notre Dame Football
Notre Dame Football Game
Notre Dame College Football
Watch Notre Dame Football
Notre Dame Football Live
Notre Dame Football Live Stream
共有していただきありがとうございます! これは私が必要とするものです。
ReplyDeleteinstagram viewer
kiss day wallpaper with quotes
ReplyDeletehappy kisss day my love quotes
message for wife on hug day
nice article judi poker online yang aman dan terpercaya
ReplyDeleteTanks for your artile. By the way https://ferdigskrevet.com/
ReplyDelete
ReplyDeleteشركة نقل اثاث من الرياض الى الاردن
شركة نقل اثاث من الرياض الى الامارات
شركة نقل اثاث من الرياض الى جدة
شركة نقل اثاث من الرياض الى الكويت
شركة نقل اثاث من الرياض الى ابها
شركة نقل اثاث من الرياض الى خميس مشيط
شركة نقل اثاث من الرياض إلي الباحة
شركة نقل اثاث من الرياض إلي مكة
شركة نقل اثاث من الرياض الى المدينة المنورة
ReplyDeleteشركة نقل اثاث من الرياض الى الطائف
شركة نقل اثاث من الرياض إلي نجران
شركة نقل اثاث من الرياض الى الدمام
شركة نقل عفش بالرياض 300 ريال
شركة نقل عفش من الرياض الى البحرين
شركة نقل اثاث من الرياض الى تبوك
شركة نقل اثاث من الرياض الى القصيم
شركة نقل اثاث من الرياض الى جازان
شركة نقل اثاث من الرياض الى الجبيل
ReplyDeleteشركة تركيب ستائر بالرياض
شركة تركيب اثاث ايكيا بالرياض
شركة نقل اثاث من الرياض الى الامارات
شركة فك وتركيب المكيفات بالرياض
نقل اثاث من الرياض الى الامارات
directvnow apk
ReplyDeletetvtap apk
redbox tv apk
hotstar apk
dstvnow apk
terrarium tv apk
netflix apk
mobdro tv apk
hdstreamz apk
HI EVERYONE
ReplyDeleteMerry Christmas & Happy New Year
Happy New Year Quotes
Merry Christmas & Happy New Year
Happy New Year Messages Poetry
Happy New Year Resolution
Happy New Year Resolutions
Happy New Year Gift Ideas
Happy New Year Wallpapers
Check Also Today Live Match
Today Live Match Streaming
Today Live Match Prediction
World Cup Live Streaming
Webroot.com/safe is a protection software solution that communicates with the cloud avoiding the hassle to manage the signature updates to deploy. for office setup visit office.com/setup
ReplyDelete18 inch weave hair
ReplyDelete20 inch weave hair
22 inch weave hair
24 inch weave hair
26 inch weave hair
Hi everyone
ReplyDeletecareer jankari hindi latest updates here
career jankari hindi latest updates here
career planning medical engineering admission job motivation jankari hindi
mbbs admission ebtrentr job updates here
B.Ed admission course job updates here
after 12 th arts job carrier vacancy
iit admission course job Indian institute of technology hindi latest updates here
veterinary Doctors admission course job hindi latest updates here
paramedical course after 10 the 12 course admission paramedical admission college
medical college in Karnataka government private medical college in Karnataka
ReplyDeletemedical college in Karnataka government private medical college in Karnataka
ReplyDeletegovernment medical college in India
medical college in Bihar
aiims in India AIIMS ALL AIIMS
ReplyDeletehttps://www-tutuapp.com/
tutuapp
tutuapp download
tutuapp apk best ios helper
tutuapp vip