1

I would like to declare a template as follows:

template <typename T>
{ 
  if objects of class T have method foo(), then 
   const int k=1
  else 
   if class has a static const int L then
    const int k=L
   else 
    const int k=0;


}

How can I do this? In general, I would like a mechanism for setting static consts based on properties of T (or typedef defined inside T).

duli
  • 1,621
  • 3
  • 16
  • 25
  • @Jan, good point of reminding me to pull back my code snippet :) – YeenFei Jun 14 '10 at 01:04
  • 2
    @Jan: I did just now. Sometimes I forget to click the arrow even if I have identified a valid answer. I realize its unfair to the responders - I will try to be better about it. – duli Jun 14 '10 at 01:12
  • 3
    1. It's about giving back; 2. People here put a lot of time and effort into their answers. The OP is well-advised to show respect for that; 3. If someone comes across the question in the future, they find the best answer right away and don't have to assume; 4. It's just how this site works. – Jan K. Jun 14 '10 at 01:38
  • @duli: Even though you probably don't, if you know all the functions you can make a text file and check to see what objects functions contain by making the constructor of a 'class T' write into a text indicating what objects contain what. –  Jun 14 '10 at 02:14
  • @Jan - "If someone comes across the question in the future, they find the best answer right away and don't have to assume." That actually IS an assumption and a rather poor one at that. More than a few times I've seen the poster accept incorrect answers. – Edward Strange Jun 14 '10 at 04:17
  • Usually, the best answers is the one with the most upvotes, and *not* necessarily the accepted one. – jalf Jun 14 '10 at 09:36

3 Answers3

5

The outer part is of course quite easy. Use boost::mpl::if_ to decide which int_ type to return from your metafunction and then access the value in it. No big deal.

The part where you try to find out if type X has a function f() is still fairly straight forward but unfortunately you'll not find a generic answer. Every time you need this kind of inspection you'll have to write a custom metafunction to find it out. Use SFINAE:

  template < typename T >
  struct has_foo
  {
    typedef char (&no)  [1];
    typedef char (&yes) [2];

    template < void (T::*)() >
    struct dummy {};

    template < typename S >
    static yes check( dummy<&S::foo> *);

    template < typename S >
    static no check( ... );

    enum { value = sizeof(check<T>(0)) == sizeof(yes)  };
  };

Edit: Oh, and create a checker for your static const L with BOOST_MPL_HAS_XXX()

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
0

Hard to say answer in current form, because problem domain isn't clear.

Still, you could try using the type traits technique, which enhances your templated class by adding parameters specific to only objects which have type T.

I'm pretty sure, that using type traits could solve your problems, still, using them and writing / making old routines generic could sometimes get tricky.

M. Williams
  • 4,945
  • 2
  • 26
  • 27
0

Using boost's enable_if some of such functionality can be implemented using traits. But since C++ lacks reflection questions like "does this class contains this method/member?" are not easy to resolve

Community
  • 1
  • 1
Ismael
  • 2,995
  • 29
  • 45