Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a warning free template function to convert basic types to string
    primarykey
    data
    text
    <p>I want to provide a templated function that converts most basic types to string. The best I have come up with so far is the following:</p> <pre><code>template&lt;typename T&gt; inline std::string anyToString(const T&amp; var) { std::ostringstream o; o &lt;&lt; var; return o.str(); } </code></pre> <p>The function can e.g. be used for the following:</p> <pre><code> class TimeError:public std::runtime_error{ public: explicit TimeError(int time):std::runtime_error(anyToString(time)), mTime(time){}; protected: int mTime; }; </code></pre> <p>The problem with anyToString and similar functions is the generation of ambiguity warnings when compiling with gcc version 4.4.3 -Wall -Wexta -Werror <code>"ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second</code>"</p> <p>To my knowledge the reason for the warning lies in the implicit conversion possibilities when calling &lt;&lt;. </p> <p>Those ambiguities are mainly generated by other templates as the following: </p> <pre><code> template&lt;typename T&gt; T&amp; operator&lt;&lt;(T&amp; out, const SymRad&amp; angle){ return out &lt;&lt; angle.deg(); } </code></pre> <p>But those have other advantages like working for several stream types. So I would like to keep them. If I turn the second template into a plain method for e.g. ostream the ambiguity is cleaned, but I'm looking for sth. that allows keeping both templates. Is there a generic function that does provide the same simplicity without generating warnings using the described options ? If not, what is the best way to <strong>locally</strong> disable the issued warning ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload