问题
I want to extend the lexical_cast
method for vector<uint>
types, but it's not working.
I tried the following code:
#include <boost/lexical_cast.hpp>
namespace boost
{
template <>
inline string lexical_cast <string>(vector<uint> source)
{
string tmp;
for (size_t i = 0; i < source.size(); ++i)
if (i < source.size() - 1)
tmp += boost::lexical_cast<string>(source[i]) + "|";
else
tmp += boost::lexical_cast<string>(source[i]);
return tmp;
}
}
I got following error:
error: template-id ‘lexical_cast’ for ‘std::string boost::lexical_cast(std::vector)’ does not match any template declaration
回答1:
Lexical cast can be extended by overloading operator<<
.
Problem is that std::vector
nor uint
are your types: they're builtin or standard library. This makes it not-okay for you to overload or specialize inside the namespace.
Real solution:
Use a strong User-Defined Type
C++ is favors strong typing:
#include <vector>
#include <ostream>
struct Source {
std::vector<uint> _data;
friend std::ostream& operator<<(std::ostream& os, Source const& s) {
bool first = true;
for(auto i : s._data) {
if (!first) os << "|";
first = false;
os << i;
}
return os;
}
};
BONUS
lexical_cast
now magically works!
Live On Coliru
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip> // for std::quoted
int main() {
Source s { {1,2,3,4,5} };
std::cout << "Source is " << s << "\n";
std::string text = boost::lexical_cast<std::string>(s);
std::cout << "Length of " << std::quoted(text) << " is " << text.length() << "\n";
}
Prints
Source is 1|2|3|4|5
Length of "1|2|3|4|5" is 9
Adapt for IO
Use a custom IO Manipulator like e.g. How do I output a set used as key for a map?
#include <ostream>
template <typename Container>
struct pipe_manip {
Container const& _data;
friend std::ostream& operator<<(std::ostream& os, pipe_manip const& manip) {
bool first = true;
for(auto& i : manip._data) {
if (!first) os << "|";
first = false;
os << i;
}
return os;
}
};
template <typename Container>
pipe_manip<Container> as_pipe(Container const& c) { return {c}; }
These also work with Boost Lexicalcast:
Live On Coliru
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <set>
#include <vector>
int main() {
std::vector<uint> s { {1,2,3,4,5} };
std::cout << "Source is " << as_pipe(s) << "\n";
std::string text = boost::lexical_cast<std::string>(as_pipe(std::set<std::string>{"foo", "bar", "qux"}));
std::cout << "Other containers work too: " << text << "\n";
}
Prints
Source is 1|2|3|4|5
Other containers work too: bar|foo|qux
来源:https://stackoverflow.com/questions/47882043/template-specialization-boostlexical-cast