事实上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
网友评论