c++ - Assert when instancing specific template specialization -
By default, a user can specifically write a template, by adding some useful error messages to the default template:
template & lt; Typename T & gt; Struct foo {static_assert (sizeof (t)! = Sizeof (T), "Please specialize in foo for your type"); }; Since always-false conditions depend on a template parameter, that firmness will be evaluated only during the template reversal, whatever we want.
Now consider the inverse case: We allow the user to use the generic version of the template, but do not allow it to use a specific rebound. For example:
template & lt; Typename T & gt; The Straight Bar {/ * Something useful for the user; The template's & lt; & Gt; Straight bar & lt; Char & gt; {Static_assert (wrong, "no, you can not instantiate time with the bar"); }; Of course this does not work because the claim is not dependent on a template parameter, even then the compilation will fail, even if the user has the bar & lt; Char & gt; / Code>. My question is: Is there any way to evaluate that kind of expression unless the template installation? Edit : A more complex example: template & lt; Typename T & gt; Struct quux {/ * Something useful for the user; / * This specialization has been defined which rejects any example of quux which uses * templates with * * templates, such as parameter * / template & lt; Template & lt; Typename & gt; Class T & gt; Structure quacks & lt; T & lt; Bool & gt; & Gt; {Static_assert ("wrong," please do not use quix with a template with the bool parameter); };
No special expertise required:
Template & lt; Typnam T & G; Structure bar {static_assert (! Std :: is_same & lt; T, char & gt; :: value, "No, you can not instantiate times with the bar"); }; Edit: In response to your edit, again the same thing: Template Template & lt; Typename & gt; Class T, Typanum U & gt; Structure quacks & lt; T & lt; U & gt; & Gt; {Static_assert (! Std :: is_same & lt; U, bool & gt; :: value, "please do not use quux with a template with bux parameter"); };
Comments
Post a Comment