Boost logo

Boost :

Subject: Re: [boost] [utility] Proposal to extract some components from Boost.Log
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2013-08-28 08:16:24


2013/8/28 Andrey Semashev <andrey.semashev_at_[hidden]>

> A special char_trait type does change the type of string_ref, but it
> doesn't actually prohibit creating a string_ref from a non-literal string.
>

Hmmm, you are right. And how about this:

#include <boost/utility/string_ref.hpp>
#include <string>

template <class Char, class Traits>
class basic_string_literal: public boost::basic_string_ref<Char, Traits> {
    typedef boost::basic_string_ref<Char, Traits> base_t;
public:
    template <std::size_t N>
    explicit basic_string_literal(const Char (&array)[N]) BOOST_NOEXCEPT
        : base_t(array, N)
    {}
};

typedef basic_string_literal<char, std::char_traits<char> > string_literal;

int main() {
    string_literal sl1("Hello word");
    // string_literal sl2(std::string("Hello word")); //
compilation error
    // string_literal sl3(std::string("Hello word").c_str()); //
compilation error
    sl1.clear();
}

> > > One question: you're using strcmp to compare type_info names on some
> > > compilers to solve problems with cross-library comparisons. If symbol
> > > visibility is configured properly, cross-library comparisons should
> work
> > > properly, shouldn't they? Or are you aware of platforms that do not
> > support
> > > cross-library comparisons at all? I'm asking because the list of such
> > > faulty platforms in your code looks overly long. E.g. on Linux
> type_info
> > > can be compared just fine, as long as the type info symbols have
> default
> > > visibility, and the comparison is faster than strcmp.
> >
> > GCC's Standard library and some other libraries had an issue. They were
> > assuming that if two dynamic libraries have same strings, then dynamic
> > linker will load those symbols to the same address. That's why they were
> > comparing types by pointers to type names instead of comparing type
> names.
> > Unfortunately assumption is not right for some platforms (ARM) and
> linkers.
>
> Can these particular configurations be detected at compile time? E.g. by
> testing for ARM target? Comparing symbol addresses works on x86 Linux, and
> I'd like to use it.
>

Oh yes, I'll need to check GCC once more. As I remember that issue was
fixed in GCC 4.5 and it is safe to use default std::type_info comparison.

-- 
Best regards,
Antony Polukhin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk