1

Here is the code I was looking, Source code :

template <typename T>
struct function_traits
    : public function_traits<decltype(&T::operator())>
{};

if we instantiate it with some functor X i.e function_traits<X>; , That will build the base class which is function_traits<decltype(&X::operator())> due to inheritance , but to build function_traits<decltype(&X::operator())> it's base also has to be built, which could be function_traits<decltype(Z)>

I understand function_traits<X> != function_traits<Z>. Isn't that recursive inheritance? 0_o. How all things is working together?

3
  • I don't understand what your question is. This isn't valid code without some other specialization of function_traits for member function pointers. Commented Dec 21, 2011 at 21:15
  • @FreakEnum : Yes, that code contains the exact specializations I was referring to. ;-] Commented Dec 21, 2011 at 21:28
  • What are you trying to do? Can you show us a minimal working example? Commented Dec 26, 2011 at 0:54

1 Answer 1

2

This is illegal code. You cannot derive from an incomplete type, and at the point you try to derive from function_traits, the type is incomplete.

struct A { typedef A type; };
struct B { typedef A type; };

template <typename T>
struct X : X<typename T::type> {};

X<B> test; // error: invalid use of incomplete type ‘struct X<A>’

The only way you could get round this is if the function_traits you are trying to derive from is a complete type. You can do this using specialisation:

struct A { typedef A type; };
struct B { typedef A type; };

template <typename T>
struct X : X<typename T::type> {};

template <>
struct X<A> {}; // X<A> is now a complete type.

X<B> test; // OK! Derives from X<A>, which is complete.

Here, X<A> is complete when you try to derive from it, so you're fine.

Sign up to request clarification or add additional context in comments.

6 Comments

here is the whole If you might wanna look github.com/kennytm/utils/blob/master/traits.hpp
@FreakEnum: As you can see, he specialises the template for various types of functions below the first definition, so he always has a complete class. I'm sure KennyTM will answer any other questions you have :-) stackoverflow.com/users/224671/kennytm
Now that made sense to me. Thank you:)
"This is illegal code." The code shown is incomplete. How can you say it is not legal? "the type is incomplete" But what's a complete type? We do not have enough information.
@curiousguy: True, but when I posted there was no link to the source, so I assumed it was the whole code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.