TITLE: restricting template arguments to only built-in types (Newsgroups: comp.lang.c++.moderated, 11 Feb 97) WALTER: Ken Walter (icedancer@ibm.net) : The more general solution is too costly in our time or money. : Is there any way to cause a compiler error or at least a deliberate run : time : error : if the class is not primitive? KUEHL: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl) Sure: Just declare a template class and specialize it for the built-in types. Then, e.g. in the destructor of the template class you want to restrict, instantiate an object of this class with the corresponding template argument. In the example below the class 'accept_only_builtins' can only be instantiated with a builtin as template argument: template class builtin_only; // I'm not sure whether this list is complete: class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; class builtin_only {}; // The next one requires partial specialization which is not yet // available with any compiler I have... template class builtin_only {} // A class which can only instantiated for a builtin type: template class accept_only_builtins { public: ~accept_only_builtins() { builtin_only(); } }; // A non-builtin type: struct foo {}; // A function attempting different instantiation: int main() { accept_only_builtins bobj; // OK accept_only_builtins fobj; // Error return 0; } If you consider pointers to be builtin (as they are by the C++ language) you would need a compiler supporting partial specialization. Other than this, the above should be a solution to your problem. Of course, if instantiating an object of type 'builtin_only' in every call to the destructor causes an efficiency problem, there are other approaches forcing an instantation of 'builtin_only', too.