Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 747 Bytes

README.md

File metadata and controls

42 lines (31 loc) · 747 Bytes

any

std::any的C++98實現,實現了大部分功能,無依賴。

CMake引入

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(CMakePrintHelpers)

set(project any)
project(${project})

set(Any_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Any)
if(Any_FOUND)
    cmake_print_variables(Any_INCLUDE_DIRS)
endif()

add_executable(${project})
target_sources(${project}
  PRIVATE src/main.cpp)

target_link_libraries(${project}
  PUBLIC Any::Any)

C++使用

#include <any.hpp>

std::vector<int> vec;
vec.push_back(1);

any a = make_any<std::vector<int>>(vec);
std::vector<int>* pvec = any_cast<std::vector<int>>(&a);
if (pvec != NULL)
{
    assert(pvec->at(0) == vec[0]);
}