Forbidding template instantiation for specific type

Discussion in 'C++' started by Marcin Kalicinski, Apr 2, 2004.

  1. Hi,

    I am modernizing a small library that is being currently in use by some
    people. In the new version I want to forbid use of a template function with
    argument of const char * (i.e. I want to get compile errors when it
    happens). For the rest of the types function should work ok.

    // A template function, in old version of the library allowed for all types
    template<typename T> void func(T t)
    {
    /* ... */
    }

    Now I want to forbid its use with const char *. For example I could use
    specialization in the following way:

    template<> void func(const char *)
    {
    const int a = 0;
    a = 1;
    }

    Is there a cleaner way of achieving the same? Expecially to get better error
    message? (ideally the error message should say that func(const char*) is
    undefined).

    Best regards,
    Marcin
     
    Marcin Kalicinski, Apr 2, 2004
    #1
    1. Advertising

  2. Marcin Kalicinski

    Buster Guest

    Marcin Kalicinski wrote:
    > Hi,
    >
    > I am modernizing a small library that is being currently in use by some
    > people. In the new version I want to forbid use of a template function with
    > argument of const char * (i.e. I want to get compile errors when it
    > happens). For the rest of the types function should work ok.
    >
    > // A template function, in old version of the library allowed for all types
    > template<typename T> void func(T t)
    > {
    > /* ... */
    > }
    >
    > Now I want to forbid its use with const char *. For example I could use
    > specialization in the following way:
    >
    > template<> void func(const char *)
    > {
    > const int a = 0;
    > a = 1;
    > }
    >
    > Is there a cleaner way of achieving the same? Expecially to get better error
    > message? (ideally the error message should say that func(const char*) is
    > undefined).


    Error messages vary across implementations, but you can try

    template <> void func (const char *)
    {
    int constraints_func_undefined_for_pointer_to_const_char [-1];
    }

    Regards,
    Buster.
     
    Buster, Apr 2, 2004
    #2
    1. Advertising

  3. Marcin Kalicinski

    Buster Guest

    Marcin Kalicinski wrote:
    > Hi,
    >
    > I am modernizing a small library that is being currently in use by some
    > people. In the new version I want to forbid use of a template function with
    > argument of const char * (i.e. I want to get compile errors when it
    > happens). For the rest of the types function should work ok.
    >
    > // A template function, in old version of the library allowed for all types
    > template<typename T> void func(T t)
    > {
    > /* ... */
    > }
    >
    > Now I want to forbid its use with const char *. For example I could use
    > specialization in the following way:
    >
    > template<> void func(const char *)
    > {
    > const int a = 0;
    > a = 1;
    > }
    >
    > Is there a cleaner way of achieving the same? Expecially to get better error
    > message? (ideally the error message should say that func(const char*) is
    > undefined).


    Oh man. That's too funny. Ignore my other post. Just don't define it!

    template <> void func (const char *);

    You'll get a link error.

    Regards,
    Buster.
     
    Buster, Apr 2, 2004
    #3
  4. Uzytkownik "Buster" <> napisal w wiadomosci
    news:c4jl18$itv$...
    > Marcin Kalicinski wrote:
    > > Hi,
    > >
    > > I am modernizing a small library that is being currently in use by some
    > > people. In the new version I want to forbid use of a template function

    with
    > > argument of const char * (i.e. I want to get compile errors when it
    > > happens). For the rest of the types function should work ok.
    > >
    > > // A template function, in old version of the library allowed for all

    types
    > > template<typename T> void func(T t)
    > > {
    > > /* ... */
    > > }
    > >
    > > Now I want to forbid its use with const char *. For example I could use
    > > specialization in the following way:
    > >
    > > template<> void func(const char *)
    > > {
    > > const int a = 0;
    > > a = 1;
    > > }
    > >
    > > Is there a cleaner way of achieving the same? Expecially to get better

    error
    > > message? (ideally the error message should say that func(const char*) is
    > > undefined).

    >
    > Oh man. That's too funny. Ignore my other post. Just don't define it!
    >
    > template <> void func (const char *);
    >
    > You'll get a link error.


    Now that's a clever idea. Thanks!

    Best regards,
    Marcin
     
    Marcin Kalicinski, Apr 2, 2004
    #4
  5. Marcin Kalicinski

    Dan Moos Guest


    > >
    > > Oh man. That's too funny. Ignore my other post. Just don't define it!
    > >
    > > template <> void func (const char *);
    > >
    > > You'll get a link error.

    >
    > Now that's a clever idea. Thanks!
    >
    > Best regards,


    I guess I don't see how that is an improvement over the OP's initial method.
    Both will confuse an unsuspecting user of the template who isn't "In the
    know". I other words, if I try to use that template with a const char *,
    and I get a link error, I'm gonna be one confused dude, and can't imagine
    coming to the conclusion that it is intended behaviour! I actually liked the
    first idea of creating an array with an informative name, and giving it a
    negative number as a size. I actually found that to be pretty clever! The
    trick is giving the array a name that makes the situation obvious, like
    "USER_this_template_does_not_work_with_const_char_pointers"
     
    Dan Moos, Apr 2, 2004
    #5
  6. Marcin Kalicinski

    Buster Guest

    Dan Moos wrote:
    >>>Oh man. That's too funny. Ignore my other post. Just don't define it!
    >>>
    >>>template <> void func (const char *);
    >>>
    >>>You'll get a link error.

    >>
    >>Now that's a clever idea. Thanks!
    >>
    >>Best regards,

    >
    >
    > I guess I don't see how that is an improvement over the OP's initial method.
    > Both will confuse an unsuspecting user of the template who isn't "In the
    > know". I other words, if I try to use that template with a const char *,
    > and I get a link error, I'm gonna be one confused dude, and can't imagine
    > coming to the conclusion that it is intended behaviour! I actually liked the
    > first idea of creating an array with an informative name, and giving it a
    > negative number as a size. I actually found that to be pretty clever! The
    > trick is giving the array a name that makes the situation obvious, like
    > "USER_this_template_does_not_work_with_const_char_pointers"


    Good point. We should use plain English. I was about to suggest
    'function_func_is_not_implemented_for_arguments_of_type_pointer_to_const_char'
    before I thought of the other idea. But the OP did ask for an error
    saying that the function was undefined.
     
    Buster, Apr 2, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    600
    Oliver Wong
    Mar 30, 2006
  2. Fernando Cuenca
    Replies:
    4
    Views:
    2,550
    Gianni Mariani
    Sep 6, 2004
  3. Thomas Maier-Komor
    Replies:
    6
    Views:
    648
    Thomas Maier-Komor
    May 19, 2005
  4. Replies:
    1
    Views:
    609
    Salt_Peter
    Dec 25, 2006
  5. Ed
    Replies:
    1
    Views:
    356
Loading...

Share This Page