Re: [sigc] libsigc++ & Forte: template operator()



Am 2004.03.20 00:25 schrieb(en) Murray Cumming:
On Fri, 2004-03-19 at 18:39, martin-ml hippogriff de wrote:
> I think it can be stripped down. The following should do:
>
> class Thing
> {
>   public:
>   Thing()
>   {}
>
>   template <class T>
>   void operator()(T a, T b)
>   {
>     T c = a + b;
>     std::cout << c << std::endl;
>   }
> };
>
> int main(int, char*[])
> {
>   Thing thing_;
>   thing_.template operator()<int>(1, 2);
> }

Thanks. That does succeed with g++ but not with my SUN Forte C++ 5.3
or
5.4. So I have made that a configure-time test. I suggest a quick
tarball release so that Damien can try it on his SUN Forte C+ 5.5.


My SUN Forte C++ 5.3 and 5.4 still get stuck on

CC -DHAVE_CONFIG_H -I.. -I.. -g -c signal.cc  -KPIC -DPIC -o
.libs/signal.o
"../sigc++/functors/functor_trait.h", line 48: Error: Function
templates
may not  have default template parameters.

This is utter nonsense. Again it is pure guesswork to figure out
what keeps the FORTE from compiling. I tried to create a test to
reproduce the error (test_functor_trait1.cc) and a possible
solution (test_functor_trait2.cc). If this fails, maybe is_base_and_derived<> is still causing troubles. To check this,
I have attached test_base_and_derived.cc. Hopefully the tests
lead to some result.

Regards,

 Martin
#include <iostream>

template <class T_type>
struct type_trait
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef const T_type& take;
  typedef T_type* pointer;
  static  T_type  instance(); /* not implemented */  
};

template <class T_type, int N>
struct type_trait<T_type[N]>
{
  typedef T_type*  type;
  typedef T_type*& pass;
  typedef const T_type*& take;
  typedef T_type** pointer;
  static  T_type*  instance(); /* not implemented */  
};

template <class T_type>
struct type_trait<T_type&>
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef T_type& take;
  typedef T_type* pointer;
  static  T_type& instance(); /* not implemented */ 
};

template <class T_type>
struct type_trait<const T_type&>
{
  typedef const T_type  type;
  typedef const T_type& pass;
  typedef const T_type& take;
  typedef const T_type* pointer;
  static  const T_type& instance(); /* not implemented */ 
};

template<>
struct type_trait<void>
{
  typedef void  type;
  typedef void  pass;
  typedef void  take;
  typedef void* pointer;
  static  void  instance(); /* not implemented */
};


/** From Esa Pulkkin:
 * Compile-time determination of base-class relationship in C++
 * (adapted to match the syntax of boost's type_traits library).
 */
template <class T_base, class T_derived>
struct is_base_and_derived
{
private:
  struct big {
    char memory[64];
  };

  struct test {
    static big  is_base_class_(...);
    static char is_base_class_(typename type_trait<T_base>::pointer);
  };

public:
  static const bool value =
    sizeof(test::is_base_class_((typename type_trait<T_derived>::pointer)0)) ==
    sizeof(char);
};

template <class T_base>
struct is_base_and_derived<T_base, T_base>
{
  static const bool value = true;
};



struct functor_base {};


template <class T_functor, bool I_derives_functor_base=is_base_and_derived<functor_base,T_functor>::value>
struct functor_trait
{
  typedef void result_type;
  typedef T_functor functor_type;
};

template <class T_functor>
struct functor_trait<T_functor,true>
{
  typedef typename T_functor::result_type result_type;
  typedef T_functor functor_type;
};

#define SIGC_FUNCTORS_HAVE_RESULT_TYPE                 \
template <class T_functor>                             \
struct functor_trait<T_functor,false>                  \
{                                                      \
  typedef typename T_functor::result_type result_type; \
  typedef T_functor functor_type;                      \
};


struct foo : public functor_base {
  typedef int result_type;

  int operator()() {
    return 1;
  }
};


int main() {
  functor_trait<foo>::result_type c = 2;
  std::cout << c << std::endl;
}
#include <iostream>

template <class T_type>
struct type_trait
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef const T_type& take;
  typedef T_type* pointer;
  static  T_type  instance(); /* not implemented */  
};

template <class T_type, int N>
struct type_trait<T_type[N]>
{
  typedef T_type*  type;
  typedef T_type*& pass;
  typedef const T_type*& take;
  typedef T_type** pointer;
  static  T_type*  instance(); /* not implemented */  
};

template <class T_type>
struct type_trait<T_type&>
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef T_type& take;
  typedef T_type* pointer;
  static  T_type& instance(); /* not implemented */ 
};

template <class T_type>
struct type_trait<const T_type&>
{
  typedef const T_type  type;
  typedef const T_type& pass;
  typedef const T_type& take;
  typedef const T_type* pointer;
  static  const T_type& instance(); /* not implemented */ 
};

template<>
struct type_trait<void>
{
  typedef void  type;
  typedef void  pass;
  typedef void  take;
  typedef void* pointer;
  static  void  instance(); /* not implemented */
};


/** From Esa Pulkkin:
 * Compile-time determination of base-class relationship in C++
 * (adapted to match the syntax of boost's type_traits library).
 */
template <class T_base, class T_derived>
struct is_base_and_derived
{
private:
  struct big {
    char memory[64];
  };

  struct test {
    static big  is_base_class_(...);
    static char is_base_class_(typename type_trait<T_base>::pointer);
  };

public:
  static const bool value =
    sizeof(test::is_base_class_((typename type_trait<T_derived>::pointer)0)) ==
    sizeof(char);
};

template <class T_base>
struct is_base_and_derived<T_base, T_base>
{
  static const bool value = true;
};



struct functor_base {};


template <class T_functor, bool I_derives_functor_base=is_base_and_derived<functor_base,T_functor>::value>
struct functor_trait;

template <class T_functor>
struct functor_trait<T_functor,false>
{
  typedef void result_type;
  typedef T_functor functor_type;
};

template <class T_functor>
struct functor_trait<T_functor,true>
{
  typedef typename T_functor::result_type result_type;
  typedef T_functor functor_type;
};


struct foo : public functor_base {
  typedef int result_type;

  int operator()() {
    return 1;
  }
};


int main() {
  functor_trait<foo>::result_type c = 2;
  std::cout << c << std::endl;
}
#include <iostream>

template <class T_type>
struct type_trait
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef const T_type& take;
  typedef T_type* pointer;
  static  T_type  instance(); /* not implemented */  
};

template <class T_type, int N>
struct type_trait<T_type[N]>
{
  typedef T_type*  type;
  typedef T_type*& pass;
  typedef const T_type*& take;
  typedef T_type** pointer;
  static  T_type*  instance(); /* not implemented */  
};

template <class T_type>
struct type_trait<T_type&>
{
  typedef T_type  type;
  typedef T_type& pass;
  typedef T_type& take;
  typedef T_type* pointer;
  static  T_type& instance(); /* not implemented */ 
};

template <class T_type>
struct type_trait<const T_type&>
{
  typedef const T_type  type;
  typedef const T_type& pass;
  typedef const T_type& take;
  typedef const T_type* pointer;
  static  const T_type& instance(); /* not implemented */ 
};

template<>
struct type_trait<void>
{
  typedef void  type;
  typedef void  pass;
  typedef void  take;
  typedef void* pointer;
  static  void  instance(); /* not implemented */
};


/** From Esa Pulkkin:
 * Compile-time determination of base-class relationship in C++
 * (adapted to match the syntax of boost's type_traits library).
 */
template <class T_base, class T_derived>
struct is_base_and_derived
{
private:
  struct big {
    char memory[64];
  };

  struct test {
    static big  is_base_class_(...);
    static char is_base_class_(typename type_trait<T_base>::pointer);
  };

public:
  static const bool value =
    sizeof(test::is_base_class_((typename type_trait<T_derived>::pointer)0)) ==
    sizeof(char);
};

template <class T_base>
struct is_base_and_derived<T_base, T_base>
{
  static const bool value = true;
};



struct functor_base {};

struct foo : public functor_base {
  typedef int result_type;

  int operator()() {
    return 1;
  }
};

struct bar {
};


int main() {
  bool a = is_base_and_derived<functor_base,foo>::value;
  bool b = is_base_and_derived<functor_base,bar>::value;
  std::cout << a << std::endl;
  std::cout << b << std::endl;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]