This might be a silly question but I would like to have it clarified none the less. Lets say I have a template function like so:
template<class T> T getValue(const char *key) const;
that returns the value as T
from internal storage where it is stored under key
(and possibly as type T already).
Now in order to use this I need to specify the template return type T
in the function call, for example:
int value = getValue<int>("myKey");
while what I would want it to do is deduce the template argument from the context, specifically the lvalue
like so:
int value = getValue("myKey"); //getValue<int>() is instantiated with int being deduced automatically from lvalue
but I am guessing that this is not possible but I am rather fuzzy as to why. I know using auto
would make it impossible for the compiler to deduce the template type but why this is as well?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…