久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='uJL5J'><tr id='uJL5J'><dt id='uJL5J'><q id='uJL5J'><span id='uJL5J'><b id='uJL5J'><form id='uJL5J'><ins id='uJL5J'></ins><ul id='uJL5J'></ul><sub id='uJL5J'></sub></form><legend id='uJL5J'></legend><bdo id='uJL5J'><pre id='uJL5J'><center id='uJL5J'></center></pre></bdo></b><th id='uJL5J'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='uJL5J'><tfoot id='uJL5J'></tfoot><dl id='uJL5J'><fieldset id='uJL5J'></fieldset></dl></div>
    <legend id='uJL5J'><style id='uJL5J'><dir id='uJL5J'><q id='uJL5J'></q></dir></style></legend>

      <small id='uJL5J'></small><noframes id='uJL5J'>

        <bdo id='uJL5J'></bdo><ul id='uJL5J'></ul>

    1. <tfoot id='uJL5J'></tfoot>
    2. 在 C++ 編譯時計算和打印階乘

      Calculating and printing factorial at compile time in C++(在 C++ 編譯時計算和打印階乘)

        <tfoot id='R1R5Y'></tfoot>

      • <small id='R1R5Y'></small><noframes id='R1R5Y'>

          <bdo id='R1R5Y'></bdo><ul id='R1R5Y'></ul>

            <i id='R1R5Y'><tr id='R1R5Y'><dt id='R1R5Y'><q id='R1R5Y'><span id='R1R5Y'><b id='R1R5Y'><form id='R1R5Y'><ins id='R1R5Y'></ins><ul id='R1R5Y'></ul><sub id='R1R5Y'></sub></form><legend id='R1R5Y'></legend><bdo id='R1R5Y'><pre id='R1R5Y'><center id='R1R5Y'></center></pre></bdo></b><th id='R1R5Y'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='R1R5Y'><tfoot id='R1R5Y'></tfoot><dl id='R1R5Y'><fieldset id='R1R5Y'></fieldset></dl></div>
              <tbody id='R1R5Y'></tbody>

              1. <legend id='R1R5Y'><style id='R1R5Y'><dir id='R1R5Y'><q id='R1R5Y'></q></dir></style></legend>

                本文介紹了在 C++ 編譯時計算和打印階乘的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..
                template<unsigned int n>
                struct Factorial {
                    enum { value = n * Factorial<n-1>::value};
                };
                
                template<>
                struct Factorial<0> {
                    enum {value = 1};
                };
                
                int main() {
                    std::cout << Factorial<5>::value;
                    std::cout << Factorial<10>::value;
                }
                

                上面的程序在編譯時計算階乘值.我想在編譯時而不是在運行時使用 cout 打印階乘值.我們如何才能在編譯時打印階乘值?

                above program computes factorial value during compile time. I want to print factorial value at compile time rather than at runtime using cout. How can we achive printing the factorial value at compile time?

                我使用的是 VS2009.

                I am using VS2009.

                謝謝!

                推薦答案

                階乘可以在編譯器生成的消息中打印為:

                The factorial can be printed in compiler-generated message as:

                template<int x> struct _;
                int main() {
                        _<Factorial<10>::value> __;
                        return 0;
                }
                

                錯誤信息:

                prog.cpp:14:32: 錯誤:聚合 ‘_<3628800> __’ 類型不完整,無法定義_::值> __;^

                prog.cpp:14:32: error: aggregate ‘_<3628800> __’ has incomplete type and cannot be defined _::value> __; ^

                這里362880010的階乘.

                在 ideone 上查看:http://ideone.com/094SJz

                See it at ideone : http://ideone.com/094SJz

                所以你在找這個嗎?

                Matthieu 需要一個聰明的技巧來打印階乘并讓編譯繼續.這是一種嘗試.它沒有給出任何錯誤,因此編譯成功并發出一個警告.

                Matthieu asked for a clever trick to both print the factorial AND let the compilation continue. Here is one attempt. It doesn't give any error, hence the compilation succeeds with one warning.

                template<int factorial> 
                struct _{ operator char() { return factorial + 256; } }; //always overflow
                int main() {
                        char(_<Factorial<5>::value>());
                        return 0;
                }
                

                編譯時帶有此警告:

                main.cpp: 在實例化 '_::operator char() [with intfactorial = 120]': main.cpp:16:39: 從這里需要main.cpp:13:48: 警告:隱式常量轉換溢出[-Woverflow] struct _{ operator char() { return factorial + 256;} };//總是溢出

                main.cpp: In instantiation of '_::operator char() [with int factorial = 120]': main.cpp:16:39: required from here main.cpp:13:48: warning: overflow in implicit constant conversion [-Woverflow] struct _{ operator char() { return factorial + 256; } }; //always overflow

                這里1205的階乘.

                ideone 上的演示:http://coliru.stacked-crooked.com/a/c4d703a670060545

                Demo at ideone : http://coliru.stacked-crooked.com/a/c4d703a670060545

                你可以寫一個很好的宏,然后用它來代替:

                You could just write a nice macro, and use it instead as:

                #define PRINT_AS_WARNING(constant) char(_<constant>())    
                
                int main() 
                {
                         PRINT_AS_WARNING(Factorial<5>::value);
                         return 0;
                }
                

                那個看起來很棒.

                這篇關于在 C++ 編譯時計算和打印階乘的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Why do two functions have the same address?(為什么兩個函數的地址相同?)
                Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                mixing templates with polymorphism(混合模板與多態性)
                When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)
                  • <tfoot id='hxQSv'></tfoot>
                    • <bdo id='hxQSv'></bdo><ul id='hxQSv'></ul>
                      <i id='hxQSv'><tr id='hxQSv'><dt id='hxQSv'><q id='hxQSv'><span id='hxQSv'><b id='hxQSv'><form id='hxQSv'><ins id='hxQSv'></ins><ul id='hxQSv'></ul><sub id='hxQSv'></sub></form><legend id='hxQSv'></legend><bdo id='hxQSv'><pre id='hxQSv'><center id='hxQSv'></center></pre></bdo></b><th id='hxQSv'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='hxQSv'><tfoot id='hxQSv'></tfoot><dl id='hxQSv'><fieldset id='hxQSv'></fieldset></dl></div>

                        <tbody id='hxQSv'></tbody>

                        1. <small id='hxQSv'></small><noframes id='hxQSv'>

                          <legend id='hxQSv'><style id='hxQSv'><dir id='hxQSv'><q id='hxQSv'></q></dir></style></legend>
                          主站蜘蛛池模板: 成在线人视频免费视频 | 日韩午夜一区二区三区 | 欧美日韩综合一区 | 久久亚洲综合 | 男女视频网站 | 成人国产午夜在线观看 | 精品久久久一区二区 | 欧美激情综合 | 成人三级网址 | 国产在线精品免费 | 免费看黄色小视频 | 久久久久成人精品亚洲国产 | 欧美激情亚洲天堂 | 91精品国产综合久久精品 | 1级黄色大片 | 亚洲欧美一区二区三区国产精品 | 三a毛片 | 亚洲天天| 视频二区在线观看 | 国产一区 | 九色在线观看 | 午夜伦理影院 | 欧美性久久 | 在线观看毛片网站 | 很很干很很日 | 久久精品国产一区二区三区不卡 | 午夜日韩精品 | 日韩欧美三级电影 | 男女视频91 | 欧美日韩国产在线观看 | 男女黄网站 | 久久精品一区二区三区四区 | 97精品国产 | 一级片在线免费播放 | 精品国产一区二区三区四区在线 | 久久中文字幕一区 | 成人精品一区二区 | 久久精品亚洲欧美日韩精品中文字幕 | 免费黄色片在线观看 | 国产精品成人一区二区 | 欧美日韩三级在线观看 |