美文网首页
C++ Traits和Policy演示累加器和累乘器

C++ Traits和Policy演示累加器和累乘器

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

    仿照 C++ Templates 《Traits and Policy Classes》这一章,改进了一下,造了一个泛型的累加器和累乘器出来。
    大家平时不要这么搞,直接用STL自带的就行。
    这个示例完全只是为了练手。
    这个示例还有一种实现方法,把initia_val放在 Accum类的accum函数里面。但是没有直接放在模板参数里面好用。
    放在模板参数里面,你可以直接使用默认实参。

    CMakeLists.txt
    其中有一些库是没有必要链接的,只需要标准C++就可以。
    C++标准大于 C++11。

    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(accum7)
    
    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})
    

    accum_traits7.hpp

    #ifndef _FREDRIC_ACCUM_TRAITS6_HPP_
    #define _FREDRIC_ACCUM_TRAITS6_HPP_
    
    template <typename T, int initial_value=0>
    class accumulation_traits;
    
    #define MK_TRAITS(source_type, dest_type) \
    template <int initial_value> \
    class accumulation_traits<source_type, initial_value> { \
        public:  \
            using AccT = dest_type;     \
            static AccT init() {    \
                return initial_value;  \
            }                       \
    };              \
    
    
    MK_TRAITS(char, int)
    MK_TRAITS(short, int)
    MK_TRAITS(int, long)
    MK_TRAITS(unsigned int, unsigned long)
    MK_TRAITS(float, double)
    
    #undef MK_TRAITS
    
    #endif
    

    sum_policy.hpp

    #ifndef _FREDRIC_SUM_POLICY_HPP_
    #define _FREDRIC_SUM_POLICY_HPP_
    
    class sum_policy {
        public:
            template <typename T1, typename T2>
            static void accumulate(T1& total, T2 const& val) {
                total += val;
            }
    };
    
    #endif
    

    mul_policy.hpp

    #ifndef _FREDRIC_MUL_POLICY_HPP_
    #define _FREDRIC_MUL_POLICY_HPP_
    
    class mul_policy {
        public:
            template <typename T1, typename T2>
            static void accumulate(T1& total, T2 const& val) {
                total *= val;
            }
    };
    #endif
    

    accum7.hpp

    #ifndef _FREDRIC_ACCUM6_HPP_
    #define _FREDRIC_ACCUM6_HPP_
    
    #include "accum_traits7.hpp"
    #include "sum_policy.hpp"
    #include "mul_policy.hpp"
    
    template <typename T,
        typename Policy = sum_policy,
        typename Traits = accumulation_traits<T>>
    class Accum {
        public:
            using AccT = typename Traits::AccT;
    
            static AccT accum(T const* beg, T const* end) {
                // 有问题,这个zero是0,加法可以,乘法乘出来是错误的
                AccT total = Traits::init();
                while(beg != end) {
                    Policy::accumulate(total, *beg);
                    ++beg;
                }
    
                return total;
            }
    };
    
    template <typename T, int initial_val=1>
    using MulAccum = Accum<T, mul_policy, accumulation_traits<T, initial_val>>;
    
    template <typename T, int initial_val=0>
    using SumAccum = Accum<T, sum_policy, accumulation_traits<T, initial_val>>;
    
    #endif
    

    main.cpp

    #include "accum7.hpp"
    #include "mul_policy.hpp"
    
    #include <iostream>
    #include <cstdlib>
    
    int main(int argc, char* argv[]) {
    
        int nums[] {1, 2, 3, 4, 5};
    
        std::cout << "The average value of the integer value is: "
            << SumAccum<int, 15>::accum(&nums[0], &nums[5]) / 5
            << "\n";
    
        std::cout << "The product of integer values is: "
            << MulAccum<int>::accum(&nums[0], &nums[5])
            << "\n";
    
        char const name[] = "templates";
        int length = sizeof(name)-1;
    
        std::cout << "The average value of the character values is: "
            << SumAccum<char>::accum(&name[0], &name[length]) / length
            << std::endl;
    
        return EXIT_SUCCESS;
    }
    

    程序输出如下,


    image.png

    相关文章

      网友评论

          本文标题:C++ Traits和Policy演示累加器和累乘器

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