美文网首页
C++使用模板特化做类型提升

C++使用模板特化做类型提升

作者: FredricZhu | 来源:发表于2021-12-21 16:48 被阅读0次

    主题思想就是用IfThenElse模板在编译期选择类型。

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    if(APPLE)
        message(STATUS "This is Apple, do nothing.")
    elseif(UNIX)
        message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
        set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
    endif(APPLE)
    
    project(promote1)
    
    add_definitions(-std=c++17)
    
    add_definitions(-g)
    
    find_package(ZLIB)
    
    find_package(glog REQUIRED)
    
    find_package(OpenCV REQUIRED )
    
    find_package(Boost REQUIRED COMPONENTS
        system
        filesystem
        serialization
        program_options
        thread
        )
    
    find_package(DataFrame REQUIRED)
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ {CMAKE_CURRENT_SOURCE_DIR}/../)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
        set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../../include/)
    endif(APPLE)
    
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
        set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set LINK_DIRS")
        set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
    endif(APPLE)
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
        set(ODBC_LIBS iodbc iodbcinst)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set LINK_DIRS")
        set(ODBC_LIBS odbc odbcinst ltdl)
    endif(APPLE)
    
    include_directories(${INCLUDE_DIRS})
    LINK_DIRECTORIES(${LINK_DIRS})
    
    file( GLOB main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) 
    
    file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp)
    
    add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
    target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
    target_link_libraries(${PROJECT_NAME}_lib  ssl crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace)
    
    foreach( main_file ${main_file_list} )
        file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
        string(REPLACE ".cpp" "" file ${filename})
        add_executable(${file}  ${main_file})
        target_link_libraries(${file} ${PROJECT_NAME}_lib)
    endforeach( main_file ${main_file_list})
    

    promotion1.hpp

    #ifndef _FREDRIC_PROMOTION1_HPP_
    #define _FREDRIC_PROMOTION1_HPP_
    
    #include "ifthenelse.hpp"
    
    // Promotion 对象逻辑,
    // 如果 T1占用内存大 取 T1,否则取T2,
    // 如果 T1 == T2, 取void
    template <typename T1, typename T2>
    class Promotion {
        public:
            using ResultT = typename IfThenElse<(sizeof(T1) > sizeof(T2)), 
                                                T1,
                                typename IfThenElse<(sizeof(T1) < sizeof(T2)),
                                                T2,
                                                void
                                >::ResultT>::ResultT;
    };
    
    #endif
    

    promotion2.hpp

    #ifndef _FREDRIC_PROMOTION2_HPP_
    #define _FREDRIC_PROMOTION2_HPP_
    #include "promotion1.hpp"
    
    // 针对相同类型的特化
    template <typename T>
    class Promotion <T, T> {
        public:
            using ResultT = T;
    };
    
    #endif
    

    promotion3.hpp

    #ifndef _FREDRIC_PROMOTION3_HPP_
    #define _FREDRIC_PROMOTION3_HPP_
    #include "promotion1.hpp"
    
    #define MK_PROMOTION(T1, T2, Tr)    \
        template <> \
        class Promotion<T1, T2> {   \
            public: \
                using ResultT = Tr; \
        };  \
        template <> \
        class Promotion<T2, T1> {   \
            public: \
                using ResultT = Tr; \
        };  \
    
    
    MK_PROMOTION(bool, char, int)
    MK_PROMOTION(bool, unsigned char, int)
    MK_PROMOTION(bool, signed char, int)
    
    #undef MK_PROMOTION
    
    #endif
    

    promotion_array.hpp

    #ifndef _FREDRIC_PROMOTION_ARRAY_HPP_
    #define _FREDRIC_PROMOTION_ARRAY_HPP_
    
    #include "promotion1.hpp"
    #include "promotion2.hpp"
    #include "promotion3.hpp"
    
    template <typename T>
    class Array {};
    
    // 针对Array的特化
    template <typename T1, typename T2>
    class Promotion<Array<T1>, Array<T2>> {
        public:
            using ResultT = Array<typename Promotion<T1, T2>::ResultT>;
    };
    
    // 针对同类型 Array的特化
    template <typename T>
    class Promotion<Array<T>, Array<T>> {
        public:
            using ResultT = Array<typename Promotion<T, T>::ResultT>;
    };
    
    #endif
    

    ifthenelse.hpp

    #ifndef _FREDRIC_IF_THEN_ELSE_HPP_
    #define _FREDRIC_IF_THEN_ELSE_HPP_
    
    // 总体模板
    template <bool Bool, typename Ta, typename Tb>
    class IfThenElse;
    
    // 偏特化
    template <typename Ta, typename Tb>
    class IfThenElse<true, Ta, Tb> {
        public:
            using ResultT = Ta;
    };
    
    // false 偏特化
    template <typename Ta, typename Tb>
    class IfThenElse<false, Ta, Tb> {
        public:
            using ResultT = Tb;
    };
    
    #endif
    

    main.cpp

    #include "promotion_array.hpp"
    
    #include <iostream>
    #include <cstdlib>
    
    #include <boost/type_index.hpp>
    
    int main(int argc, char* argv[]) {
        typename Promotion<char, bool>::ResultT res_t1;
        typename Promotion<Array<double>, Array<int>>::ResultT res_t2;
    
        std::cout << boost::typeindex::type_id_runtime(res_t1) << std::endl;
        std::cout << boost::typeindex::type_id_runtime(res_t2) << std::endl;
        
        return EXIT_SUCCESS;
    }
    

    程序输出如下,


    image.png

    相关文章

      网友评论

          本文标题:C++使用模板特化做类型提升

          本文链接:https://www.haomeiwen.com/subject/evmxqrtx.html