0
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?

cppython
  • 1,209
  • 3
  • 20
  • 30

2 Answers2

5

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
}
jrok
  • 54,456
  • 9
  • 109
  • 141
  • i try to understand this line "unwrap u2". decltype will return void type. and T is foo, right? if T is foo, but foo is not typename. I got lost... please help again – cppython Aug 05 '13 at 20:06
  • 1
    @cppython: `int a; decltype(foo(a))` would be void, but `decltype(foo)` is not. It is whatever type `foo` *is*, not what it *returns when called*. `foo` is "a function taking an `int&` and returning `void`". The decltype in the sample code does the same as `typedef void bar(int &); unwrap u2;`, where the type `bar` is introduced as "the type of a function taking an `int&` and returning `void`". As it turns out, `foo` is of type `bar`. – mars Aug 05 '13 at 21:19
  • i still dont get it. why will std::cout < and .please advise again, thanks so much.i need another sample – cppython Aug 06 '13 at 07:15
  • @cppython `decltype` is a compile time specifier that gives you the type of the entity/expression that's used as its operand. If you give it an entity, it gives you the type that the entity was declared with. F.ex. `int i; decltype(i)` gives you `int`. If you give it an expression, it gives you the type of the result of that expression: `decltype(8*5)` gives you `int` again. In the example in my answer, the result will be the same if you write `void(int&)` instead of `decltype(foo)`. – jrok Aug 06 '13 at 07:23
  • `std::cout << u2.i` outputs 1 because the template is specialized for functions types `void(T)` and 1 is the value of its static member `i`. If template gets instantiated with some other type, the value of `i` is 0. I used `decltype` in the example just to show that `foo` really fits the partial specialization. – jrok Aug 06 '13 at 07:24
  • 1
    @cppython Sorry, the confusion seems to be about decltype, which was introduced in the rather new `c++11` standard. `decltype(5+4) a;` means `int a;`, `decltype (5.4+4) a;` means `float a;`, and `std::vector v;` is a vector of whatever datatype it necessary to hold the result of `x*y`, which can be matrix multiplication or some custom object. So `unwrap` is `unwrap` which matches both `unwrap` for `T=int&` and `unwrap` for `T=void(int&)`, but the first one wins because it is more specific. – mars Aug 06 '13 at 10:02
  • Thanks for jrok and mars.I learned how the decltype work online. and now i know how to connect the decltype user case to this question. one more thing, I think I never saw before is Void(int&). I saw void http://stackoverflow.com/questions/1043034/what-does-void-mean-in-c-c-and-c :There are 3 basic ways that void is used: Function argument: int myFunc(void) -- the function takes nothing. Function return value: void myFunc(int) -- the function returns nothing Generic data pointer: void* data -- 'data' is a pointer to data of unknown type, and cannot be dereferenced. but none of them match – cppython Aug 06 '13 at 17:59
  • my question is what void(int&) is? I guess is a type. a type of function which return is void and take one input parameter. am i right? – cppython Aug 06 '13 at 18:07
1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621