c++ - Can my code use a 'T' or 'const T &' specialization, whichever is available? -


we working inhouse library features stringbuilder class used turn list of variablevalue objects string. variablevalue objects can constructed arbitrary types (by specializing convertvariable template function). here's code describes scenario:

struct variablevalue {   // construct 'variablevalue' object, variant type can represent values of   // 1 of 4 types: string, number (integer), boolean , floating point.   explicit variablevalue( const std::string &serializeddata );    // getters, typesafe; yield exception when calling wrong getter.   const std::string &asstring() const;   bool asboolean() const;   // ..      // convert variablevalue object string   static std::string converttostring( const variablevalue &v ); };  // template specialized user types can casted // variablevalue object template <typename t> variablevalue convertvariable( t v );  // helper class 'concatenate' multiple variablevalue objects single string. class stringbuilder { public:   const std::string &result() const;    template <class t>   stringbuilder &operator<<( t v ) {     return *this << convertvariable( v );   }  private:   std::ostringstream m_stream; };  template <> inline stringbuilder &stringbuilder::operator<<( const variablevalue &v ) {   m_stream << variablevalue::converttostring( v );   return *this; } 

this woreds well. clients had provide appropriate specialization convertvariable template (our library provides plenty of specializations various types) , stringbuilder can used. almost.

the problem doesn't work types not copyable. template functions take argument value. , in case of convertvariable template it's quite expensive change signature (because there quite lot of specializations). though can make stringbuilder::operator<< template take const t &, won't since convertvariable instantiation called t (since reference-to-const part stripped while deducing template types). if fix specifying type explicitely, in:

class stringbuilder { public:   // ...    template <class t>   stringbuilder &operator<<( const t &v ) {     return *this << convertvariable<const t &>( v );   } }; 

the linker complain because no longer finds old specializations (like e.g. template <> variablevalue convertvariable( int )) since looks specializations take reference-to-const.

does know how can adjust stringbuilder class can pass non-copyable objects (that is, objects type neither allows copy construction nor copy assignment) operator<< function?

i'm not quite sure answer of help, it's worth trying. post, tend think appropriate solution change signature of convertvariable. expensive because there lot of specialization, think free depending on way chose 'specialize'.

this article offers nice guideline these kind of things :

moral #1: if want customize function base template , want customization participate in overload resolution (or, used in case of exact match), make plain old function, not specialization. and, if provide overloads, avoid providing specializations.

[...]

for thing, function template specializations don't overload. means specializations write not affect template gets used, runs counter people intuitively expect. after all, if had written nontemplate function identical signature instead of function template specialization, nontemplate function selected because it's considered better match template.

indeed, instead of specializing type uncopyableclass, use overloading :

variablevalue convertvariable( const uncopyableclass &t ) { /* ... */ } 

it's not specialization overload, , should work expected. note stringbuilder::operator<< must take const reference parameter.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

c# - How to execute a particular part of code asynchronously in a class -

c# - Asterisk click to call -