Change boost::error_info visibility

Viewed 123

I am trying to compile using -fvisibility=hidden but can not figure out how to change the visibility of a boost::error_info typedef can someone enlighten me? No matter where I put the DVEM_EXPORT in the typedef line compiler rejects it and nothing else short of editing the boost header to add the attribute there fixes the runtime problem.

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

A typedef does not declare a type.

The type declaration carries your default - hidden - so any alias name for an instance of that type (such as errinfo_data_type) is also hidden.

You are right that modifying the Boost header is the most direct way. But you can also do it non-intrusively as long as the Boost header doesn't override visibility¹

So you can simply forward declare the same type as in 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");
}

¹ which it doesn't

Related