#Template instantiation

29 messages · Page 1 of 1 (latest)

broken ridge
#

I have doubt about this snippet taken from C++ Templates: The complete Guide.
How come foo parameter type is not void**. I find it confusing since I'm assuming when we call foo here
and pass vp as parameter, isn't void* the actual type of vp, so it should deduce void foo(void**) instead?

spice patrolBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

undone shale
# broken ridge I have doubt about this snippet taken from C++ Templates: The complete Guide. Ho...

well... when you call the function

foo(vp);

this does not always mean that the type T will be substituted with decltype(vp) which here is void*, C++ is trying to find the best match for the template, and here the best match is T = void

if you really want to be explicit and ban any form of template deduction or guessing, you can do this instead:

foo<void*>(vp);

and now the type T will be guaranteed to be void* but then T* will be void** and your vp parameter cannot be inplicitly converted to void** so you will get a compilation error, you gotta use &vp instead

broken ridge
#

ahh

#

i understandit better now, ty

undone shale
#

so in C++98 you were forced to do shit like

#
int arr[] = {2,5,4,7,1};
std::sort<int*>(arr, arr+5);
broken ridge
#

there should be a long set rules for arguments match im guessing, my brain just directly substituted types

undone shale
#

very long, very boring

#

learn templates by writing code instead pls

broken ridge
#

agree

#

i am

#

was consulting the book out of curiosity

undone shale
lyric meadow
#

i think the main thing you need to keep in mind is the above

undone shale
#

have you ever asked yourself "that template T, what is T exactly?"

#

@broken ridge

broken ridge
#

no, honestly

undone shale
#

as you begin learning more about templates you will probably hit this question one day

#

the approach I use is

#

write a compilation error in the function

#

compile

#

and in the compilation error it show all of the templates substituted 🙂

spice patrolBOT
#

@broken ridge Has your question been resolved? If so, run !solved :)

broken ridge
#

!solved