Thread: How to handle illegal template arguments?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    How to handle illegal template arguments?

    Is there some good way to handle illegal template arguments? Say for example you have a template class
    Code:
    template<int x, int y> my_class {...}
    and you don't want the sum of x and y to be 53 (just an example), or you have another class
    Code:
    template<class T> my_class_2 {...}
    where you don't want T to be a floating point type? (Ideally using numeric_limits to validate T)

    Is it possible to make the declarations
    Code:
    my_class<10, 43> a;
    my_class_2<double> b;
    create compilation errors because of the incompatible template arguments?
    Come on, you can do it! b( ~_')

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    If you want to prevent floating-point types, you can use template specialization:
    Code:
    template <typename T>
    class My_Class
    {
       /* ... */
    };
    
    template <>
    class My_Class <float>
    {
       /* Private constructor and assignment operator, perhaps */
    };
    I'm not sure if your first question is possible to do. I think it might become possible using concepts in C++0x.
    Last edited by rudyman; 07-28-2008 at 11:00 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The simplest way for the first might be to get boost and use BOOST_STATIC_ASSERT (template metaprogramming tutorials should show how to roll your own):

    Code:
    #include <boost/static_assert.hpp>
    
    template <int A, int B>
    struct X
    {
        BOOST_STATIC_ASSERT(A + B != 53);
        //...
    };
    
    int main()
    {
        X<10, 43> bad; //won't compile
        X<10, 20> good;
    }
    (The error message is quite incomprehensible with MingW, but if you go to the error line you should be able to figure out what is wrong.)
    Last edited by anon; 07-28-2008 at 10:20 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are forms of those kinds of forced errors where you can specify some text that will show up in the compiler error. Something like, "Value cannot add up to 53" cannot be converted to bool or something random like that. I know the Loki library uses that, and I'd imagine something similar would be available in Boost's library as well.

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thanks, I will look in boost's libraries. What Daved writes about seems like a quite handy function, else BOOST_STATIC_ASSERT seems to do just fine. Thanks!
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function argument deduce
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2008, 08:56 PM
  2. Template overloading?
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 03:21 PM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM