Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: ok the title of your question seems to be misleading</p> <blockquote> <p>"I want a class which takes two parameters in its constructor. The first can be either an int, double or float, so , and the second is always a string literal "my string", so I guess const char * const."</p> </blockquote> <p>It looks like you're trying to achieve:</p> <pre><code>template&lt;typename T&gt; class Foo { public: Foo(T t, const char* s) : first(t), second(s) { // do something } private: T first; const char* second; }; </code></pre> <p>This would work for any type, for the first parameter: <code>int</code>, <code>float</code>, <code>double</code>, whatever.</p> <p>Now if you really want to restrict the type of the first parameter to be only <code>int</code>, <code>float</code> or <code>double</code>; you can come up with something more elaborate like</p> <pre><code>template&lt;typename T&gt; struct RestrictType; template&lt;&gt; struct RestrictType&lt;int&gt; { typedef int Type; }; template&lt;&gt; struct RestrictType&lt;float&gt; { typedef float Type; }; template&lt;&gt; struct RestrictType&lt;double&gt; { typedef double Type; }; template&lt;typename T&gt; class Foo { typedef typename RestrictType&lt;T&gt;::Type FirstType; public: Foo(FirstType t, const char* s) : first(t), second(s) { // do something } private: FirstType first; const char* second; }; int main() { Foo&lt;int&gt; f1(0, "can"); Foo&lt;float&gt; f2(1, "i"); Foo&lt;double&gt; f3(1, "have"); //Foo&lt;char&gt; f4(0, "a pony?"); } </code></pre> <p>If you remove the comment on the last line, you'll effectively get a compiler error.</p> <hr> <p>String literals are not allowed by C++2003</p> <p>ISO/IEC 14882-2003 §14.1:</p> <blockquote> <p><strong>14.1 Template parameters</strong></p> <p>A non-type template-parameter shall have one of the following (optionallycv-qualified) types:</p> <p>— integral or enumeration type, </p> <p>— pointer to object or pointer to function, </p> <p>— reference to object or reference to function, </p> <p>— pointer to member. </p> </blockquote> <p>ISO/IEC 14882-2003 §14.3.2:</p> <blockquote> <p><strong>14.3.2 Template non-type arguments</strong></p> <p>A template-argument for a non-type, non-template template-parameter shall be one of: </p> <p>— an integral constant-expression of integral or enumeration type; or</p> <p>— the name of a non-type template-parameter; or </p> <p>— the address of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as &amp; id expression where the &amp; is optional if the name refers to a function or array, or if the corresponding template-parameter is a reference; or</p> <p>— a pointer to member expressed as described in 5.3.1.</p> <p>[Note:A string literal (2.13.4) does not satisfy the requirements of any of these categories and thus is not an acceptable template-argument.</p> <p>[Example: </p> </blockquote> <pre><code>template&lt;class T, char* p&gt; class X { //... X(); X(const char* q) { /* ... */ } }; X&lt;int,"Studebaker"&gt; x1; //error: string literal as template-argument char p[] = "Vivisectionist"; X&lt;int,p&gt; x2; //OK </code></pre> <blockquote> <p>—end example] —end note] </p> </blockquote> <p>And it looks like it's not going to change in the upcoming C++0X, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf" rel="nofollow noreferrer">see the current draft 14.4.2 Template non-type arguments</a>.</p>
 

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