Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-如何键入alias、typedef或wrap boost::variant?_C++_Templates_Boost_Boost Variant - Fatal编程技术网

C++ C++;-如何键入alias、typedef或wrap boost::variant?

C++ C++;-如何键入alias、typedef或wrap boost::variant?,c++,templates,boost,boost-variant,C++,Templates,Boost,Boost Variant,我有一个现有的代码库,可以很好地利用Boost。特别是,它在一些地方使用了不同口味的boost::variant: typedef boost::variant<double, int, unsigned int, size_t, bool> MBVariant; typedef boost::variant<double, float> FPVariant; // ... typedef boost::variant MBVariant; typedef boost:

我有一个现有的代码库,可以很好地利用Boost。特别是,它在一些地方使用了不同口味的
boost::variant

typedef boost::variant<double, int, unsigned int, size_t, bool> MBVariant;
typedef boost::variant<double, float> FPVariant;
// ...
typedef boost::variant MBVariant;
typedef boost::variant FPVariant;
// ...
它还使用其他增强功能,如:

typedef boost::shared_ptr<int> MyPtr;
typedef boost::shared_ptr MyPtr;
我正在将这段代码移植到一个不支持Boost的新平台,但必须与支持Boost的平台一起维护。因此,我创建了一个“Boost wrapping”层,它将帮助消除Boost依赖性:

#ifdef USE_BOOST
# include <boost/shared_ptr.hpp>
# include <boost/variant.hpp>

template <class T>
using my_shared_ptr = boost::shared_ptr<T>

// how to wrap variant?
// template <typename T0_, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)>
// using my_variant = boost::variant<T0_, ???>;

#else
# include <memory>

// in the absence of Boost, use something else like std::shared_ptr:
template <class T>
using my_shared_ptr = std::shared_ptr<T>

// wrap variant with something non-Boost
template <typename T0, /* uh... */>
class variant { /* ... */ };

#endif
\ifdef使用BOOST
#包括
#包括
模板
使用my_shared_ptr=boost::shared_ptr
//如何包装变体?
//模板
//使用my_variant=boost::variant;
#否则
#包括
//在没有Boost的情况下,使用std::shared\u ptr之类的其他方法:
模板
使用my_shared_ptr=std::shared_ptr
//用非增压的东西包装变型
模板
类变量{/*…*/};
#恩迪夫
现在,客户机代码可以使用my_shared_ptr或my_variant,具体取决于是否为目标平台定义了use_BOOST:

typedef my_variant<double, int, unsigned int, size_t, bool> MBVariant;
typedef my_variant<double, float> FPVariant;
typedef my_shared_ptr<int> MyPtr;
typedef my_variant MBVariant;
typedef my_variant FPVariant;
typedef my_shared_ptr MyPtr;
我的问题是,
boost::variant
是一个类模板,它以递归类型列表的形式接受任意数量的参数类型。我不确定typedefs或类型别名是否(或如何)可以解决这个问题

为此,是否有一种通用的方法包装
boost::variant

我知道我可以使用简单的typedef为
boost::variant
特定的参数集提供新的类型名称,而且我目前没有太多的数据,所以这是一种可能性,但我对类型别名的一般方法非常感兴趣,比如
boost::variant
,同时尽可能避免使用预处理器,因为我需要将其集成到命名空间层次结构中

template <typename... T>
using my_variant = boost::variant<T...>
请注意,在这两种情况下,您都会遇到与ADL和专业化相关的问题。只要使用typedefs,就不能隐藏条件的存在


相反,您可以/应该编写自己的接口头并交换实现文件

您的编译器的C++11功能有多好?出于好奇,哪个平台不支持Boost?我提供了一个定制的Clang-3.0(禁用了-std=c++11标志,因此只有标准的c++0x功能可用)。它是针对虚拟LLVM CPU的,并且标准库的实现有限(例如,它缺少所有iostream内容,等等),这就是为什么Boost有很多问题。同时,我试图让Boost正常工作,但目前看来效果不太好……顺便说一句,我不能完全确定我是否理解C++11对Clang的全部支持——似乎有些功能默认支持,有些功能需要指定-std=C++11。我不明白界线在哪里。我所知道的是,在这个定制的clang版本中,我没有可用的-std=c++11开关,但是其他c++11功能,如
auto
,范围为,类型别名可以正常工作。例如,强类型枚举不起作用。我最初的想法是,您可能能够使用可变模板包装boost::Variant,但如果您没有C++11支持,这将不起作用。隐藏boost/非boost依赖项不是一种更干净的方法吗?
namespace library
{
    #if defined(HAS_BOOST)
        using boost::variant;
    #else
        using my_utils::variant;
    #endif
}