• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            有效利用標準庫提供的type_traits,讓程序在編譯時作出分支選擇

            讓程序中的簡單if-else在編譯期間決定

            ex. 1 /*
             2  * =====================================================================================
             3  *
             4  *       Filename:  3.cpp
             5  *
             6  *    Description:  
             7  *
             8  *        Version:  1.0
             9  *        Created:  03/01/2016 11:38:53 AM
            10  *       Revision:  none
            11  *       Compiler:  gcc
            12  *
            13  *         Author:  shih (Hallelujah), sh19871122@gmail.com
            14  *   Organization:  
            15  *
            16  * =====================================================================================
            17  */
            18 
            19 #include <stdio.h>
            20 #include <stdint.h>
            21 #include <iostream>
            22 #include <type_traits>
            23 
            24 template<typename T>
            25 struct is_swapable
            26 {
            27     static const bool value = std::is_integral<T>::value && sizeof(T) >= 2;
            28 };
            29 
            30 template<typename T>
            31 T byte_swap(T value, std::true_type)
            32 {
            33    uint8_t *bytes = reinterpret_cast<uint8_t *>(&value);
            34    for (std::size_t i = 0; i < sizeof(T)/2; ++i)
            35    {   
            36        uint8_t v = bytes[i];
            37        bytes[i] = bytes[sizeof(T) - 1 - i]; 
            38        bytes[sizeof(T) -1 -i] = v;
            39    }   
            40    return value;
            41 }
            42 
            43 template<typename T>
            44 T byte_swap(T value, std::false_type)
            45 {
            46     return value;
            47 }
            48 
            49 template<typename T>
            50 T byte_swap(T value)
            51 {
            52     return byte_swap(value, std::integral_constant<bool, is_swapable<T>::value>());
            53 }
            54 
            55 int main(int argc, const char *argv[])
            56 {
            57     int a = 0x11223344;
            58     long b = 0x4455221112345678;
            59     std::cout << std::hex << a << " " << b << std::endl;
            60     std::cout << std::hex << byte_swap(a) << " " << byte_swap(b) << std::endl;
            61     uint8_t c = 0x11;
            62     char *d = "hello world";
            63     std::cout << std::hex << byte_swap(c) << " " << byte_swap(d) << std::endl;
            64     return 0;
            65 }
            66 

            posted on 2016-04-28 17:50 Hallelujah 閱讀(1330) 評論(0)  編輯 收藏 引用 所屬分類: C++學習

            <2016年4月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆分類(15)

            隨筆檔案(14)

            最新隨筆

            搜索

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲色欲久久久久综合网 | 久久这里有精品视频| 99久久精品免费国产大片| 成人精品一区二区久久| 色诱久久av| 999久久久免费精品国产| 久久99国产亚洲高清观看首页| 国产精品免费久久久久影院 | 色综合久久无码五十路人妻| 亚洲欧美成人综合久久久| 国产精品久久久久久一区二区三区| 嫩草影院久久99| 久久久国产打桩机| 狠狠综合久久综合中文88| 久久久久久久亚洲Av无码| 久久久久久久国产免费看| 人人狠狠综合久久88成人| 热re99久久精品国产99热| 精品多毛少妇人妻AV免费久久 | 中文精品久久久久人妻| 久久国产精品久久久| 亚洲午夜久久久久妓女影院| 国产午夜精品理论片久久| 午夜天堂av天堂久久久| 免费精品久久久久久中文字幕| 久久久久亚洲AV无码永不| 久久久久久久女国产乱让韩| 国产成人精品久久一区二区三区av | 亚洲欧美成人综合久久久| 天堂无码久久综合东京热| 国产精品免费久久| 成人午夜精品久久久久久久小说| 色欲av伊人久久大香线蕉影院| 日本精品久久久久影院日本| 狠狠精品久久久无码中文字幕| 久久精品男人影院| 久久久青草久久久青草| 老司机国内精品久久久久| 亚洲综合精品香蕉久久网97| 狠色狠色狠狠色综合久久| 99久久中文字幕|