我很乐意得到并建议如何以"二维方式"处理boost :: variant.听起来很奇怪,但让我的代码说更多(希望如此):
我编写了一个名为Parameter的类:
template<typename PARAM_TYPE, typename DATA_TYPE=double>
class Parameter : public quantity<PARAM_TYPE, DATA_TYPE>
{
...
}
Run Code Online (Sandbox Code Playgroud)
上面定义的参数的示例用法:
Parameter<si::length, double> SampleParameter1;
Parameter<si::dimensionless, short> SampleParameter2;
Run Code Online (Sandbox Code Playgroud)
当我试图通过上面的例子解释时,我可以使用boost::units::si::???
和不同的数据类型double, short, int
等定义几个参数类型.
我的目标是构建一个std::map
可以存储任何Parameter
类型实例的容器(如上所示).
因此我宣布:
typedef boost::variant<Parameter<si::dimensionless, short>, Parameter<si::length, double> > SupportedParameterTypes;
std::map<int, SupportedParameterTypes> myMapStorage;
Run Code Online (Sandbox Code Playgroud)
这很好用,但有一个很大的缺点我想解决 - 我必须定义参数类型的每一个组合我想支持SupportedParameterTypes
上面定义的类型.
我的想法是定义boost :: mpl :: vector,保留我想支持的所有参数类型:
typedef boost::mpl::vector<si::dimensionless, si::length> ParameterTypes;
Run Code Online (Sandbox Code Playgroud)
另一方面,支持所有可能的参数数据类型:
typedef boost::mpl::vector<short, int, float, double> ParameterDataTypes;
Run Code Online (Sandbox Code Playgroud)
我遇到了麻烦:
typedef typename boost::make_variant_over<ParameterTypes>::type ParameterTypeVariants;
typedef typename boost::make_variant_over<ParameterDataTypes>::type ParameterDataVariants;
typedef boost::variant<Parameter<ParameterTypeVariants, ParameterDataVariants> …
Run Code Online (Sandbox Code Playgroud) 我正在分析以下代码片段,并试图详细了解它:
template<typename FUNCTION, typename... ARGUMENTS>
auto ThreadPool::add( FUNCTION&& Function, ARGUMENTS&&... Arguments ) -> std::future<typename std::result_of<FUNCTION(ARGUMENTS...)>::type>
{
using PackedTask = std::packaged_task<typename std::result_of<FUNCTION(ARGUMENTS...)>::type()>;
auto task = std::make_shared<PackedTask>(std::bind(std::forward<FUNCTION>(Function), std::forward<ARGUMENTS>(Arguments)...));
// get the future to return later
auto ret = task->get_future();
{
std::lock_guard<std::mutex> lock{jobsMutex};
jobs.emplace([task]() { (*task)(); });
}
// let a waiting thread know there is an available job
jobsAvailable.notify_one();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我对此几乎没有任何疑问std::packaged_task
.
正如可以在看到add(...)
方法体,实例std::packaged_task
- task
是范围与方法执行结束时结束局部变量.返回值ret
的std::future
类型由复制返回.的值ret
被给予了的task
(这是本地).所以一旦方法执行完成,task
超出范围,所以 …
Is it possible to overload the operator new to be constexpr function? Something like:
constexpr void * operator new( std::size_t count );
Run Code Online (Sandbox Code Playgroud)
The reason why would be to execute constexpr function within the overloaded operator body where count argument value would be an input data... As the operator is invoked by:
SomeClass * foo = new SomeClass();
Run Code Online (Sandbox Code Playgroud)
The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)
) So the count can be considered …
我在设置共享库的可见性时遇到问题。我想寻求帮助以解决我的问题,如下所述:
我有一堆源文件,我想将它们构建为共享库:
+Base
|_Parameter
| |_Parameter.h
| |_Parameter_Exception.h
| |_Parameter_Exception.cpp
|_Data
| |_DataInput.h
| |_DataInput_Exception.h
| |_DataInput_Exception.cpp
...
Run Code Online (Sandbox Code Playgroud)
那里还有一些文件,但我认为它不会影响我的问题描述。
我正在使用 CMake 来构建这个共享库。这是我处理构建库的 CMakeLists.txt 的一部分。
project( Base )
cmake_minimum_required(VERSION 3.5
set( Base_HEADERS
Parameter/Parameter.h
Parameter/Parameter_Exception.h
Data/DataInput.h
Data/DataInput_Exception.h
)
set( Base_SOURCES
Parameter/Parameter_Exception.cpp
Data/DataInput_Exception.cpp
)
set( LIBRARY_OUTPUT_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
add_library( Base SHARED ${Base_SOURCES} ${Base_HEADERS} )
generate_export_header( ${PROJECT_NAME} EXPORT_FILE_NAME ${PROJECT_NAME}_Export.h )
Run Code Online (Sandbox Code Playgroud)
此设置构建 libBase.so 共享库没有任何问题。值得一提的是,我已经设置了一些在链接期间使用的可见性相关标志:
set( CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined -fvisibility=hidden -fvisibility-inlines-hidden" )
Run Code Online (Sandbox Code Playgroud)
如上面部分列出的基本库 CMakeLists.txt 中的配置:
-fvisibility=hidden
) 中的设置隐藏例如一块 …
c++ ×3
c++11 ×3
boost ×1
boost-mpl ×1
c++17 ×1
constexpr ×1
future ×1
gcc ×1
if-constexpr ×1
visibility ×1