edit This is not a duplicate of Undefined references to a static member of a class . The issue has been addressed in this issue. problems (which I explain below). Here I am looking for another solution from those proposed in the answers to these questions (which implies changing the declaration / definition of the constexpr variable that needs to be used), mainly by adding the definition in the compilation unit).
I created a small make_string() template make_string() function to generate a std::string from any number of io-able arguments as follows.
using std::ostringstream; // just for this example inline ostringstream&write(ostringstream&ostr, const char*x) { if(x) ostr<<x; return ostr; } template<class T> inline ostringstream&write(ostringstream&ostr, T const&x) { ostr<<x; return ostr; } inline ostringstream&write(ostringstream&ostr) noexcept { return ostr; } template<class T, class... R> inline ostringstream&write(ostringstream&ostr, T const&x, R&&... r) { return write(write(ostr,x), std::forward<R>(r)...); } inline std::string make_string(const char*text) { return {text?text:""}; } inline std::string make_string(std::string const&text) { return {text}; } template<typename T> inline auto make_string(T var) -> decltype(std::to_string(var)) { return std::to_string(var); } template<class... Args> inline std::string make_string(Args&&... args) { ostringstream ostr; write(ostr,std::forward<Args>(args)...); return std::move(ostr.str()); }
Now it works very well and can be used as
throw std::runtime_error(make_string("offset=",offset," > max_offset =", max_offset"));
However, when printing elements of the static constexpr class, a problem arises, as in
class foo { static constexpr int max_offset=some_value;
This leads to an error during the connection. The reason is because make_string takes all its arguments by reference, including static constexpr max_offset . As a result, a link to foo::max_offset will be required when linking, see Also .
How can I avoid this problem without giving up the idea of make_string() ? (Perhaps you can replace the variational template with a variable macro, but I would consider it to be some kind of regression.) There must be a way to make_string to accept its arguments by value or by reference, depending on the type (so that built-in types can be accepted by value). How?
c ++ c ++ 11 templates argument-passing variadic-templates
Walter Mar 14 '14 at 9:31 2014-03-14 09:31
source share