【问题标题】:Change boost::error_info visibility更改 boost::error_info 可见性
【发布时间】:2017-10-06 01:20:16
【问题描述】:

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

#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;

【问题讨论】:

    标签: c++ gcc boost visibility


    【解决方案1】:

    typedef 不声明类型。

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

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

    所以你可以简单地转发声明与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");
    }
    

    ¹它没有

    【讨论】:

    • /Users/david/Projects/dvmon/src/common/Exceptions.h:7:50:致命错误:变量模板是 C++14 扩展 [-Wc++14-extensions]模板 class DVEM_EXPORT error_info; :(
    • @DavidvanLaatum 那是什么编译器?你可以在现场演示中看到 C++11 编译得很好。
    • GCC 4.9x 甚至GCC 4.4.x 相同
    • 对不起,我被跨越库边界绊倒了;)我放入的顶级标头位于未定义 DVEM_EXPORT 的不同库中
    • 因为 DVEM_EXPORT 未定义,所以将其作为类名
    猜你喜欢
    • 2011-04-16
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2014-04-23
    • 2017-08-04
    • 2012-04-01
    相关资源
    最近更新 更多