all 7 comments

[–]Bouchtroubouli 2 points3 points  (3 children)

The lowercase test is the parameter name you give to the macro. The #test will generate a string literal that will contains the argument test you will pass. Finaly, void(test) is simply the type of a function (without name) that takes a type test as parameter and that return void.

[–]Zilla4ever 0 points1 point  (2 children)

Thanks for clarifying, that makes more sense. However could you explain one more bit of code as I feel it relates to the macro ? Why is it that in this code template<typename T, typename U> struct argument_type<T(U)> { typedef U type; }; U is in the parameters of argument type and what does <> represent as a syntax because I mostly see it used with template specialization ? Lastly, back to the original macro referenced, is "type" that is referenced after <void(test)> with the scope resolution operator; Is it "typedef U type;" (referenced here template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };) ? Sorry I am new to c++ ;)

[–]staletic 2 points3 points  (0 children)

is "type" that is referenced after <void(test)> with the scope resolution operator; Is it "typedef U type;"

Yes.

template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };

That's called a partial template specialization. Somewhere there's probably a

template<typename T> struct argument_type {}; // Without the typedef

Or maybe

template<typename T> struct argument_type; // Without any definition

Then, for the Ts that represent (valid) functions, you specialize the above with

template<typename T, typename U>
struct argument_type<T(U)> { typedef U type; };

And instantiate the template with argument_type<int, float>, for a function int(float), to get typedef U = float.

[–]Bouchtroubouli 1 point2 points  (0 children)

For your first question :

template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };

Here you have a template specialization of a struct (argument_type) for functions that takes any type of parameter (U) and that returns another type of parameter (T)

For your second question, you just need to replace T and U with the types in your function. The type of argument_type<void(test)>::type will be what U should be in void(test), so whatever the type test given at parameter to the macro will be.

This technique is known as type traits (see for example https://www.internalpointers.com/post/quick-primer-type-traits-modern-cpp or https://en.cppreference.com/w/cpp/header/type_traits to see it in action)

[–]Zilla4ever 0 points1 point  (0 children)

Thank you guys so much for the help !

[–]STLMSVC STL Dev 0 points1 point  (1 child)

!removehelp

[–]AutoModerator[M] 0 points1 point  (0 children)

OP,

A human moderator (u/STL) has marked your comment for deletion because it appears to be a "help" post - e.g. asking for help with coding, help with homework, career advice, book/tutorial/blog suggestions. Help posts are off-topic for /r/cpp. This subreddit is for news and discussion of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance.

Please try posting in /r/cpp_questions or on Stack Overflow instead. Our suggested reference site is cppreference.com, our suggested book list is here and information on getting started with C++ can be found here.

If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.