Boost logo

Boost :

Subject: Re: [boost] how to distinguish between const char* and const char[N]
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-19 19:26:58


At Mon, 20 Dec 2010 00:21:26 +0100,
Jochen Wilhelmy wrote:
>
> Hi!
>
> is there a (possibly boost powered) way of distinguishing between
> const char* and const char[N]?
>
> e.g.
>
> const char* foo = "foo";
> bar(foo);
> bar("foo");
>
> The second call to bar should extract the length at compile time, e.g.
>
> template <size_t length> void bar(const char (&data)[length]) {...}
>
> The problem is that in the presence of the first variant (void
> bar(const char*))
> the second variant does not get called.

  // untested!
  #include <boost/enable_if.hpp>
  #include <boost/type_traits/is_same.hpp>
  #include <boost/type_traits/remove_pointer.hpp>

  typedef whatever1 returntype1;
  typedef whatever2 returntype2;

  template <class T>
  typename boost::enable_if<
      boost::is_same<
          const typename boost::remove_pointer<T>::type
        , char const
>,
  returntype1>::type
  int bar(T);

  template <unsigned N>
  returntype2 bar(char const(&)[N]);

HTH,

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk