美文网首页
更通用的concepts,支持带参数的函数模板约束

更通用的concepts,支持带参数的函数模板约束

作者: FredricZhu | 来源:发表于2022-04-09 21:08 被阅读0次

事实上concepts比虚函数更加灵活。
本例的例子展示一个drawable的concept可以同时接收带一个参数的draw函数,也可以接收带两个参数的draw函数。
当然这种设计本身也很蛋疼,很少见,但是我们就是支持这么骚操作。
代码如下,
CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
    set(CMAKE_MACOSX_RPATH 1)
    set(CMAKE_PREFIX_PATH /Users/aabjfzhu/software/vcpkg/ports/cppwork/vcpkg_installed/x64-osx/share )
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(multi_param_static_poly)

set(CMAKE_CXX_STANDARD 20)

add_definitions(-g)

find_package(ZLIB)

find_package(glog REQUIRED)

find_package(OpenCV REQUIRED )

find_package(OpenSSL REQUIRED)

find_package(restclient-cpp 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}/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/ ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${CMAKE_CURRENT_SOURCE_DIR}/)
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}/*.cpp) 

file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http1/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  ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/utils/*.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  OpenSSL::SSL OpenSSL::Crypto restclient-cpp libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl)

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

main.cpp

// 静态多态
#include <iostream>
#include <type_traits>
#include <tuple>

// 希望模板类型 s 有个 draw方法
template <typename T>
concept drawable_single = requires(T s) {
    s.draw(1);
};

template <typename T>
concept drawable_double = requires(T s) {
    s.draw(1, 1);
};

template <typename T>
concept drawable = drawable_single<T> || drawable_double<T>;

class Rectangle {
    public:
        auto draw(int width, int height) const {
            std::cout << "Draw an rectangle, Width: "<< width << " height: " << height << "\n";
        }
};

class Circle {
    public:
        auto draw(int radius) const {
            std::cout << "Draw a circle, radius: "<< radius << "\n";
        }
};

class Square {
    public:
        auto draw(int width) const {
            std::cout << "Draw a square, width: " << width << "\n";
        }
};

template <size_t Index=0, drawable... Ts>
constexpr auto drawAll(std::tuple<Ts ...> const& shapes) {
    if constexpr(Index != sizeof...(Ts)) {
        auto shape = std::get<Index>(shapes);
        // 如果是 Rectangle类型,直接draw rectangle
        if constexpr(std::is_same<decltype(shape), Rectangle>::value) {
            shape.draw(4, 3);
        // 否则是 Square 或者 Circle类型, draw square或者circle
        } else {
            shape.draw(3);
        }
        // Recursively draw all shapes
        drawAll<Index+1>(shapes);
    }
}


int main(int argc, char* argv[]) {
    drawAll(std::make_tuple(Rectangle{}, Circle{}, Square{}));
    return EXIT_SUCCESS;
}

程序输出如下,


圖片.png

相关文章

  • 更通用的concepts,支持带参数的函数模板约束

    事实上concepts比虚函数更加灵活。本例的例子展示一个drawable的concept可以同时接收带一个参数的...

  • 深入理解C++11 2.11 模板函数的默认模板参数

    C++98中,类模板支持默认模板参数,但是函数模板并不支持,C++11则支持了函数模板的默认参数。如: 并且,类模...

  • GEEKBAND C++ STL与模板编程

    C++ 函数模板 函数模板的声明和模板函数的生成 1.函数模板的声明 函数模板可以用来创建一个通用的函数,以支持多...

  • C++ 模板

    函数模板 函数模板,是可以创建一个通用的函数,可以支持多种形参。 用关键字 template 来定义, 在函数模板...

  • C++11 模板元编程 - 模板的编译期计算

    和函数求值类似,当模板的形参被实参替换时,模板会进行编译期计算。由于模板的参数支持类型参数和非类型参数,所以模板在...

  • ★10.关于可变参数模板

    可变参数函数模板 可变参数类模板 可变参数函数模板的使用 转发参数包

  • 【C++ Templates(11)】深入模板基础

    参数化声明 C++现在支持四种基本模板:类模板,函数模板,变量模板(variable template),别名模板...

  • 14/15

    约束模板友元 模板类的外边定义一个模板函数,在模板类中将模板函数具体化为模板类的友元函数 非约束模板友元 模板类中...

  • C++ Primer 之函数模板

    函数模板是通用的函数描述,通过将类型作为参数传递给模板,使编译器生成可用具体的类型的函数。类似于: 对于不同类型使...

  • 11 深入模板基础

    参数化声明 C++现在支持四种原始模板:函数模板、类模板、C++14引入的变量模板(variable templa...

网友评论

      本文标题:更通用的concepts,支持带参数的函数模板约束

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