Why can't compiler deduce the return types?Storing C++ template function definitions in a .CPP fileWhy can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?C++: No matching function call when calling tuple_transpose functionwith template constructor,template argument deduction/substitution failed,why?Why can't the compiler deduce my template value from the function argument?std::enable_if predicated on std::is_convertible not deducing template correctlyDoes the C++ standard allow for an uninitialized bool to crash a program?
What does "Puller Prush Person" mean?
Do VLANs within a subnet need to have their own subnet for router on a stick?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Problem of parity - Can we draw a closed path made up of 20 line segments...
The magic money tree problem
Minkowski space
Prove that NP is closed under karp reduction?
How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
"You are your self first supporter", a more proper way to say it
Why does Kotter return in Welcome Back Kotter?
What typically incentivizes a professor to change jobs to a lower ranking university?
How is it possible to have an ability score that is less than 3?
The Two and the One
Is this a crack on the carbon frame?
How do I create uniquely male characters?
How old can references or sources in a thesis be?
If I cast Expeditious Retreat, can I Dash as a bonus action on the same turn?
What's the output of a record cartridge playing an out-of-speed record
Dragon forelimb placement
What's the point of deactivating Num Lock on login screens?
LaTeX closing $ signs makes cursor jump
How to format long polynomial?
strToHex ( string to its hex representation as string)
Why can't compiler deduce the return types?
Storing C++ template function definitions in a .CPP fileWhy can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?C++: No matching function call when calling tuple_transpose functionwith template constructor,template argument deduction/substitution failed,why?Why can't the compiler deduce my template value from the function argument?std::enable_if predicated on std::is_convertible not deducing template correctlyDoes the C++ standard allow for an uninitialized bool to crash a program?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Can compiler not deduce the return type of a function if the return type happens to be a template parameter? Or I am making mistake in following code.
#include <iostream>
template <typename TT>
TT retBoolFail(bool a)
return true;
template <typename TT>
void retBoolSuccess(bool a, TT& ret)
ret = true;
return;
int main()
bool ret;
retBoolSuccess(true, ret); // Success
retBoolFail(true); // Failure
return 0;
line 'RetBoolFail(true)' fails with following error.
-*- mode: compilation; default-directory: "~/work/c++/tupleTemplate/" -*-
Compilation started at Thu Mar 21 16:52:50
g++ -c simpletemp.cc -std=c++1z -g; g++ -o simpletemp simpletemp.o
simpletemp.cc: In function ‘int main()’:
simpletemp.cc:17:21: error: no matching function for call to ‘retBoolFail(bool)’
retBoolFail(true); // Failure
^
simpletemp.cc:4:4: note: candidate: template<class TT> TT retBoolFail(bool)
TT retBoolFail(bool a) {
^
simpletemp.cc:4:4: note: template argument deduction/substitution failed:
simpletemp.cc:17:21: note: couldn't deduce template parameter ‘TT’
retBoolFail(true); // Failure
^
Compilation finished at Thu Mar 21 16:52:50
Thanks.
c++ templates
add a comment |
Can compiler not deduce the return type of a function if the return type happens to be a template parameter? Or I am making mistake in following code.
#include <iostream>
template <typename TT>
TT retBoolFail(bool a)
return true;
template <typename TT>
void retBoolSuccess(bool a, TT& ret)
ret = true;
return;
int main()
bool ret;
retBoolSuccess(true, ret); // Success
retBoolFail(true); // Failure
return 0;
line 'RetBoolFail(true)' fails with following error.
-*- mode: compilation; default-directory: "~/work/c++/tupleTemplate/" -*-
Compilation started at Thu Mar 21 16:52:50
g++ -c simpletemp.cc -std=c++1z -g; g++ -o simpletemp simpletemp.o
simpletemp.cc: In function ‘int main()’:
simpletemp.cc:17:21: error: no matching function for call to ‘retBoolFail(bool)’
retBoolFail(true); // Failure
^
simpletemp.cc:4:4: note: candidate: template<class TT> TT retBoolFail(bool)
TT retBoolFail(bool a) {
^
simpletemp.cc:4:4: note: template argument deduction/substitution failed:
simpletemp.cc:17:21: note: couldn't deduce template parameter ‘TT’
retBoolFail(true); // Failure
^
Compilation finished at Thu Mar 21 16:52:50
Thanks.
c++ templates
2
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14
add a comment |
Can compiler not deduce the return type of a function if the return type happens to be a template parameter? Or I am making mistake in following code.
#include <iostream>
template <typename TT>
TT retBoolFail(bool a)
return true;
template <typename TT>
void retBoolSuccess(bool a, TT& ret)
ret = true;
return;
int main()
bool ret;
retBoolSuccess(true, ret); // Success
retBoolFail(true); // Failure
return 0;
line 'RetBoolFail(true)' fails with following error.
-*- mode: compilation; default-directory: "~/work/c++/tupleTemplate/" -*-
Compilation started at Thu Mar 21 16:52:50
g++ -c simpletemp.cc -std=c++1z -g; g++ -o simpletemp simpletemp.o
simpletemp.cc: In function ‘int main()’:
simpletemp.cc:17:21: error: no matching function for call to ‘retBoolFail(bool)’
retBoolFail(true); // Failure
^
simpletemp.cc:4:4: note: candidate: template<class TT> TT retBoolFail(bool)
TT retBoolFail(bool a) {
^
simpletemp.cc:4:4: note: template argument deduction/substitution failed:
simpletemp.cc:17:21: note: couldn't deduce template parameter ‘TT’
retBoolFail(true); // Failure
^
Compilation finished at Thu Mar 21 16:52:50
Thanks.
c++ templates
Can compiler not deduce the return type of a function if the return type happens to be a template parameter? Or I am making mistake in following code.
#include <iostream>
template <typename TT>
TT retBoolFail(bool a)
return true;
template <typename TT>
void retBoolSuccess(bool a, TT& ret)
ret = true;
return;
int main()
bool ret;
retBoolSuccess(true, ret); // Success
retBoolFail(true); // Failure
return 0;
line 'RetBoolFail(true)' fails with following error.
-*- mode: compilation; default-directory: "~/work/c++/tupleTemplate/" -*-
Compilation started at Thu Mar 21 16:52:50
g++ -c simpletemp.cc -std=c++1z -g; g++ -o simpletemp simpletemp.o
simpletemp.cc: In function ‘int main()’:
simpletemp.cc:17:21: error: no matching function for call to ‘retBoolFail(bool)’
retBoolFail(true); // Failure
^
simpletemp.cc:4:4: note: candidate: template<class TT> TT retBoolFail(bool)
TT retBoolFail(bool a) {
^
simpletemp.cc:4:4: note: template argument deduction/substitution failed:
simpletemp.cc:17:21: note: couldn't deduce template parameter ‘TT’
retBoolFail(true); // Failure
^
Compilation finished at Thu Mar 21 16:52:50
Thanks.
c++ templates
c++ templates
asked Mar 22 at 0:06
UnSatUnSat
5781621
5781621
2
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14
add a comment |
2
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14
2
2
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14
add a comment |
3 Answers
3
active
oldest
votes
When the name of a template is used as a function to be called, template argument deduction uses only the function arguments and the template function signature, and not the function body.
One reason this rule would be difficult to change in the Standard is because of how things work if the name is overloaded. The fuller picture there is:
Name lookup finds a set of functions and function templates.
For each function template in the set, template arguments are determined from explicit template arguments, deduction, and/or default template arguments. If deduction fails, or if substituting the determined template arguments into the function type does something invalid, the function template is just discarded from the overload set as non-viable.
For every function signature (whether from a template or not), a check is done to see whether the function parameters and function arguments match up. If not, the function is not viable and is discarded from the overload set.
Overload resolution compares the remaining viable functions. For most of the process, function types from templates are treated just like non-template functions, but there are a few final tie-breaker rules specific to function template specializations.
If overload resolution selected a function template specialization and the context is one that requires a function definition to exist, then the function body is also instantiated to produce that definition. If substituting the template arguments parameters into the function body does something invalid, the program is ill-formed.
So there's a distinction here between if and when a function template's function type is instantiated, and when the function body is instantiated, and the results of an invalid template parameter usage in the two cases. Deducing from the function body would muddle this up.
But there are some other situations where a template parameter in a function template return type can be involved in template argument deduction. (This list might not be exhaustive.)
If a pointer to function or reference to function is initialized from a function template name (or its address for the pointer case), the return types are involved in template argument deduction:
template <typename TT>
TT f();
unsigned int (&func_ptr)() = f; // TT deduced as unsigned int
int g(double (*)());
int g_of_f = g(f); // TT deduced as double
A class or class template can have a conversion function template. Then some uses of an expression with that class type can implicitly use that template, which requires deducing the template arguments in its return type (which is also the thing following the operator
keyword in its declarations).
class A
public:
template <typename TT>
operator std::shared_ptr<TT>() const;
;
std::shared_ptr<int> p = A; // TT deduced as int
Also note that a function or function template using a "placeholder type" as its return type (the return type contains the keyword auto
) does deduce its return type from the body's return
statements (that are not skipped because of "if constexpr
"). Though a function without a template-head is still just one function with one specific type, not a function template, and this deduction is a separate step from any template argument deductions.
auto retBool(bool a)
return true; // return type is bool
template <typename T>
constexpr const auto* constify(T* ptr)
return ptr; // return type is const T* (T* if T is already const)
add a comment |
No, deduction only occurs within the parameter list. You can use auto
instead or just make it a non-template function with a hard-coded return type.
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
add a comment |
Type deduction doesn't work for return types only for function template arguments.
In your case you have to use
retBoolFail<bool>(true);
You have to hint the compiler to deduce the type of return type. Even using auto
in the function template will still necessitate you to hint the compiler at the calling site.
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290985%2fwhy-cant-compiler-deduce-the-return-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When the name of a template is used as a function to be called, template argument deduction uses only the function arguments and the template function signature, and not the function body.
One reason this rule would be difficult to change in the Standard is because of how things work if the name is overloaded. The fuller picture there is:
Name lookup finds a set of functions and function templates.
For each function template in the set, template arguments are determined from explicit template arguments, deduction, and/or default template arguments. If deduction fails, or if substituting the determined template arguments into the function type does something invalid, the function template is just discarded from the overload set as non-viable.
For every function signature (whether from a template or not), a check is done to see whether the function parameters and function arguments match up. If not, the function is not viable and is discarded from the overload set.
Overload resolution compares the remaining viable functions. For most of the process, function types from templates are treated just like non-template functions, but there are a few final tie-breaker rules specific to function template specializations.
If overload resolution selected a function template specialization and the context is one that requires a function definition to exist, then the function body is also instantiated to produce that definition. If substituting the template arguments parameters into the function body does something invalid, the program is ill-formed.
So there's a distinction here between if and when a function template's function type is instantiated, and when the function body is instantiated, and the results of an invalid template parameter usage in the two cases. Deducing from the function body would muddle this up.
But there are some other situations where a template parameter in a function template return type can be involved in template argument deduction. (This list might not be exhaustive.)
If a pointer to function or reference to function is initialized from a function template name (or its address for the pointer case), the return types are involved in template argument deduction:
template <typename TT>
TT f();
unsigned int (&func_ptr)() = f; // TT deduced as unsigned int
int g(double (*)());
int g_of_f = g(f); // TT deduced as double
A class or class template can have a conversion function template. Then some uses of an expression with that class type can implicitly use that template, which requires deducing the template arguments in its return type (which is also the thing following the operator
keyword in its declarations).
class A
public:
template <typename TT>
operator std::shared_ptr<TT>() const;
;
std::shared_ptr<int> p = A; // TT deduced as int
Also note that a function or function template using a "placeholder type" as its return type (the return type contains the keyword auto
) does deduce its return type from the body's return
statements (that are not skipped because of "if constexpr
"). Though a function without a template-head is still just one function with one specific type, not a function template, and this deduction is a separate step from any template argument deductions.
auto retBool(bool a)
return true; // return type is bool
template <typename T>
constexpr const auto* constify(T* ptr)
return ptr; // return type is const T* (T* if T is already const)
add a comment |
When the name of a template is used as a function to be called, template argument deduction uses only the function arguments and the template function signature, and not the function body.
One reason this rule would be difficult to change in the Standard is because of how things work if the name is overloaded. The fuller picture there is:
Name lookup finds a set of functions and function templates.
For each function template in the set, template arguments are determined from explicit template arguments, deduction, and/or default template arguments. If deduction fails, or if substituting the determined template arguments into the function type does something invalid, the function template is just discarded from the overload set as non-viable.
For every function signature (whether from a template or not), a check is done to see whether the function parameters and function arguments match up. If not, the function is not viable and is discarded from the overload set.
Overload resolution compares the remaining viable functions. For most of the process, function types from templates are treated just like non-template functions, but there are a few final tie-breaker rules specific to function template specializations.
If overload resolution selected a function template specialization and the context is one that requires a function definition to exist, then the function body is also instantiated to produce that definition. If substituting the template arguments parameters into the function body does something invalid, the program is ill-formed.
So there's a distinction here between if and when a function template's function type is instantiated, and when the function body is instantiated, and the results of an invalid template parameter usage in the two cases. Deducing from the function body would muddle this up.
But there are some other situations where a template parameter in a function template return type can be involved in template argument deduction. (This list might not be exhaustive.)
If a pointer to function or reference to function is initialized from a function template name (or its address for the pointer case), the return types are involved in template argument deduction:
template <typename TT>
TT f();
unsigned int (&func_ptr)() = f; // TT deduced as unsigned int
int g(double (*)());
int g_of_f = g(f); // TT deduced as double
A class or class template can have a conversion function template. Then some uses of an expression with that class type can implicitly use that template, which requires deducing the template arguments in its return type (which is also the thing following the operator
keyword in its declarations).
class A
public:
template <typename TT>
operator std::shared_ptr<TT>() const;
;
std::shared_ptr<int> p = A; // TT deduced as int
Also note that a function or function template using a "placeholder type" as its return type (the return type contains the keyword auto
) does deduce its return type from the body's return
statements (that are not skipped because of "if constexpr
"). Though a function without a template-head is still just one function with one specific type, not a function template, and this deduction is a separate step from any template argument deductions.
auto retBool(bool a)
return true; // return type is bool
template <typename T>
constexpr const auto* constify(T* ptr)
return ptr; // return type is const T* (T* if T is already const)
add a comment |
When the name of a template is used as a function to be called, template argument deduction uses only the function arguments and the template function signature, and not the function body.
One reason this rule would be difficult to change in the Standard is because of how things work if the name is overloaded. The fuller picture there is:
Name lookup finds a set of functions and function templates.
For each function template in the set, template arguments are determined from explicit template arguments, deduction, and/or default template arguments. If deduction fails, or if substituting the determined template arguments into the function type does something invalid, the function template is just discarded from the overload set as non-viable.
For every function signature (whether from a template or not), a check is done to see whether the function parameters and function arguments match up. If not, the function is not viable and is discarded from the overload set.
Overload resolution compares the remaining viable functions. For most of the process, function types from templates are treated just like non-template functions, but there are a few final tie-breaker rules specific to function template specializations.
If overload resolution selected a function template specialization and the context is one that requires a function definition to exist, then the function body is also instantiated to produce that definition. If substituting the template arguments parameters into the function body does something invalid, the program is ill-formed.
So there's a distinction here between if and when a function template's function type is instantiated, and when the function body is instantiated, and the results of an invalid template parameter usage in the two cases. Deducing from the function body would muddle this up.
But there are some other situations where a template parameter in a function template return type can be involved in template argument deduction. (This list might not be exhaustive.)
If a pointer to function or reference to function is initialized from a function template name (or its address for the pointer case), the return types are involved in template argument deduction:
template <typename TT>
TT f();
unsigned int (&func_ptr)() = f; // TT deduced as unsigned int
int g(double (*)());
int g_of_f = g(f); // TT deduced as double
A class or class template can have a conversion function template. Then some uses of an expression with that class type can implicitly use that template, which requires deducing the template arguments in its return type (which is also the thing following the operator
keyword in its declarations).
class A
public:
template <typename TT>
operator std::shared_ptr<TT>() const;
;
std::shared_ptr<int> p = A; // TT deduced as int
Also note that a function or function template using a "placeholder type" as its return type (the return type contains the keyword auto
) does deduce its return type from the body's return
statements (that are not skipped because of "if constexpr
"). Though a function without a template-head is still just one function with one specific type, not a function template, and this deduction is a separate step from any template argument deductions.
auto retBool(bool a)
return true; // return type is bool
template <typename T>
constexpr const auto* constify(T* ptr)
return ptr; // return type is const T* (T* if T is already const)
When the name of a template is used as a function to be called, template argument deduction uses only the function arguments and the template function signature, and not the function body.
One reason this rule would be difficult to change in the Standard is because of how things work if the name is overloaded. The fuller picture there is:
Name lookup finds a set of functions and function templates.
For each function template in the set, template arguments are determined from explicit template arguments, deduction, and/or default template arguments. If deduction fails, or if substituting the determined template arguments into the function type does something invalid, the function template is just discarded from the overload set as non-viable.
For every function signature (whether from a template or not), a check is done to see whether the function parameters and function arguments match up. If not, the function is not viable and is discarded from the overload set.
Overload resolution compares the remaining viable functions. For most of the process, function types from templates are treated just like non-template functions, but there are a few final tie-breaker rules specific to function template specializations.
If overload resolution selected a function template specialization and the context is one that requires a function definition to exist, then the function body is also instantiated to produce that definition. If substituting the template arguments parameters into the function body does something invalid, the program is ill-formed.
So there's a distinction here between if and when a function template's function type is instantiated, and when the function body is instantiated, and the results of an invalid template parameter usage in the two cases. Deducing from the function body would muddle this up.
But there are some other situations where a template parameter in a function template return type can be involved in template argument deduction. (This list might not be exhaustive.)
If a pointer to function or reference to function is initialized from a function template name (or its address for the pointer case), the return types are involved in template argument deduction:
template <typename TT>
TT f();
unsigned int (&func_ptr)() = f; // TT deduced as unsigned int
int g(double (*)());
int g_of_f = g(f); // TT deduced as double
A class or class template can have a conversion function template. Then some uses of an expression with that class type can implicitly use that template, which requires deducing the template arguments in its return type (which is also the thing following the operator
keyword in its declarations).
class A
public:
template <typename TT>
operator std::shared_ptr<TT>() const;
;
std::shared_ptr<int> p = A; // TT deduced as int
Also note that a function or function template using a "placeholder type" as its return type (the return type contains the keyword auto
) does deduce its return type from the body's return
statements (that are not skipped because of "if constexpr
"). Though a function without a template-head is still just one function with one specific type, not a function template, and this deduction is a separate step from any template argument deductions.
auto retBool(bool a)
return true; // return type is bool
template <typename T>
constexpr const auto* constify(T* ptr)
return ptr; // return type is const T* (T* if T is already const)
answered Mar 22 at 1:08
aschepleraschepler
53.6k580131
53.6k580131
add a comment |
add a comment |
No, deduction only occurs within the parameter list. You can use auto
instead or just make it a non-template function with a hard-coded return type.
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
add a comment |
No, deduction only occurs within the parameter list. You can use auto
instead or just make it a non-template function with a hard-coded return type.
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
add a comment |
No, deduction only occurs within the parameter list. You can use auto
instead or just make it a non-template function with a hard-coded return type.
No, deduction only occurs within the parameter list. You can use auto
instead or just make it a non-template function with a hard-coded return type.
answered Mar 22 at 0:15
0x499602D20x499602D2
68.4k27122208
68.4k27122208
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
add a comment |
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
does parameter list mean function parameter list or template parameter list?
– UnSat
Mar 22 at 0:21
1
1
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
@UnSat Function parameter list.
– 0x499602D2
Mar 22 at 15:32
add a comment |
Type deduction doesn't work for return types only for function template arguments.
In your case you have to use
retBoolFail<bool>(true);
You have to hint the compiler to deduce the type of return type. Even using auto
in the function template will still necessitate you to hint the compiler at the calling site.
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
add a comment |
Type deduction doesn't work for return types only for function template arguments.
In your case you have to use
retBoolFail<bool>(true);
You have to hint the compiler to deduce the type of return type. Even using auto
in the function template will still necessitate you to hint the compiler at the calling site.
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
add a comment |
Type deduction doesn't work for return types only for function template arguments.
In your case you have to use
retBoolFail<bool>(true);
You have to hint the compiler to deduce the type of return type. Even using auto
in the function template will still necessitate you to hint the compiler at the calling site.
Type deduction doesn't work for return types only for function template arguments.
In your case you have to use
retBoolFail<bool>(true);
You have to hint the compiler to deduce the type of return type. Even using auto
in the function template will still necessitate you to hint the compiler at the calling site.
answered Mar 22 at 0:18
aepaep
573313
573313
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
add a comment |
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
I think, if we call the way you suggested, it is just template parameter type substitution and not type deduction.
– UnSat
Mar 22 at 0:24
1
1
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
Correct, it's not deduction in a strict sense. You hint the compiler by substituting the type.
– aep
Mar 22 at 0:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290985%2fwhy-cant-compiler-deduce-the-return-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The signature of a function doesn't depend on its return value, so the compiler has nothing to deduce with.
– Mark Ransom
Mar 22 at 0:14