cppython
cppython

Reputation: 1279

c++ boost template type and non-type

template <class Target>
struct unwrap_predicate<void (Target)>
{
    typedef is_convertible<mpl::_, Target> type;
};

this a piece of code from Boost library for whole program see: http://www.boost.org/doc/libs/release/boost/parameter/preprocessor.hpp

I dont understand Target. the first Target next to Class. this is a type parameter. the second one void(Target) looks like non-type parameter to me. how can a parameter acted as type and non-type.I got confused about this two lines. Can anyone help?

Upvotes: 0

Views: 179

Answers (2)

jrok
jrok

Reputation: 55425

the second one void(Target) looks like non-type parameter to me.

It's not, Target is just part of a type here - a function type that returns void.

What you have there is a partial template specialization for any function type that takes one parameter and returns void.

Example:

template <typename T>
struct unwrap { static const int i = 0; };

template<typename T>
struct unwrap<void(T)> { static const int i = 1; };

void foo(int&);

int main()
{
    unwrap<int> u1;
    unwrap<decltype(foo)> u2;
    std::cout << u1.i << u2.i; // prints 01
}

Upvotes: 5

Some programmer dude
Some programmer dude

Reputation: 409472

It's a function type.

void (Target)

is the type of a function returning void (i.e. nothing) and taking a single argument of type Target.

Upvotes: 1

Related Questions