Rohit
Rohit

Reputation: 98

Create a type which exhibits exact same behavior as STL container std::set

I have following typedefs :

typedef std::pair<std::string, std::string> iprange;
typedef std::set<iprange> iprange_set;

When I try to write lexical_cast around iprange and iprange_set with some other types such as Json::Value or std:string compiler is giving error since a typedef is just an alias not a real type, so it can't be overloaded.

is there a way to have a type that exhibit the properties of existing type without typedef ?

The result I am looking for is that I should be able to declare variables using that type and also use it as parameter in boost::lexical_cast

Posting code

#include <iostream>
#include <string>
#include <set>
#include <boost/lexical_cast.hpp>
using namespace std;

typedef set<string> ipaddr_list;

namespace boost {
    template<>
        string lexical_cast(const ipaddr_list* const & list)
        {
            return "qwe"; //actually code do convert ipaddr_list to string
        }
};

int main()
{
    ipaddr_list l;
    l.insert("45.5.5.5-56.6.6.6");

    string s = boost::lexical_cast<string>(l);
}

Compiler giving following error:

In file included from test.cpp:4: /usr/include/boost/lexical_cast.hpp:349:13: error: implicit instantiation of undefined template 'boost::STATIC_ASSERTION_FAILURE' BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_left_shift< std::basic_ostream< type >, T >::value), ^ and some more

Upvotes: 0

Views: 94

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

You provided a specialization for lexical_cast(const ipaddr_list*) - a function taking a pointer. But you are not calling that specialization - you are not in fact passing a pointer to lexical_cast. Instead, you are calling a general-purpose implementation, which tries to send ipaddr_list instance to a stream, but naturally fails to find a suitable overload of operator<<. This is what the assert is telling you.

Upvotes: 3

Related Questions