c++ - 更改 boost::error_info 可见性

标签 c++ gcc boost visibility

我正在尝试使用 -fvisibility=hidden 进行编译,但无法弄清楚如何更改 boost::error_info typedef 的可见性有人可以启发我吗?无论我将 DVEM_EXPORT 放在 typedef 行中的什么位置,编译器都会拒绝它,除了编辑 boost header 以添加该属性之外,别无他法,可以解决运行时问题。

#ifdef....
#define DVEM_EXPORT __attribute__((visibility("default")))
....
class DVEM_EXPORT UnsupportedDataTypeException : public dv::BaseException<> {};
struct DVEM_EXPORT errinfo_data_type_ {};
typedef boost::error_info<errinfo_data_type_, std::string> errinfo_data_type;

最佳答案

typedef 不声明类型。

类型声明包含您的默认值 - 隐藏 - 因此该类型实例的任何别名(例如 errinfo_data_type)也被隐藏。

您说得对,修改 Boost header 是最直接的方法。但您也可以以非侵入方式执行此操作,只要 Boost header 不覆盖可见性¹

因此您可以简单地向前声明与 boost/exception/error_info.hpp 中相同的类型:

Live On Coliru

#define DVEM_EXPORT __attribute__((visibility("default")))

namespace boost { 
    template <class Tag,class T> class DVEM_EXPORT error_info; 
}

#include <boost/exception/all.hpp>
#include <stdexcept>

namespace dv {
    template <typename=void>
    struct DVEM_EXPORT BaseException : virtual boost::exception, virtual std::exception {
    };
}

#include <string>
class DVEM_EXPORT UnsupportedDataTypeException : public dv::BaseException<> {};
struct DVEM_EXPORT errinfo_data_type_ {};
typedef boost::error_info<errinfo_data_type_, std::string> errinfo_data_type;

DVEM_EXPORT void foo() {
    throw UnsupportedDataTypeException() << errinfo_data_type("bar");
}

¹ 它没有

关于c++ - 更改 boost::error_info 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46596933/

相关文章:

c++ - 使用 decltype 的函数参数类型

c - 在文件开头重写

c++ - 将 asio read_some 转换为异步版本

c - 如何在 c 中乘以 32 位整数

c++ - 未存储命令行参数(使用 boost)

c++ - boost asio 和 WinSock.h 已经包含在内

c++ - 基于数值(正、负、零)实现条件表达式的最佳方式

c++ - 什么时候使用 exit() 而不是 return?

c++ - 当我尝试对数组的内容使用函数时,控制台中没有任何显示

gcc - Matlab 卡尔曼/usr/bin/ld : cannot find -lstdc++