Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template variables

35 views
Skip to first unread message

Doug Mika

unread,
Jul 13, 2015, 2:40:18 PM7/13/15
to
If a template variable type can be deduced from the variables provided in the constructor of a template class, then is it needed? I thought it wasn't, but when in the following program (below) I replace the line:
map<int, string, function<bool(const int&, const int&)>>
mapIntToString1(ReverseSort<int>);
with:
map<int, string>
mapIntToString1(ReverseSort<int>);
the code no longer compiles? Why isn't the compiler able to deduce from the submitted function ReverseSort<int> that the template variable type is bool(*)(const int&, const int&)? Is the compiler only smart enough to deduce this for template functions?


#include <iostream>
#include <string>
#include <map>
#include <functional>

using namespace std;

template<typename KeyType>
bool ReverseSort(const KeyType& key1, const KeyType& key2){
return (key1 > key2);
}

int main(int argc, char** argv) {

//The Program
map<int, string, function<bool(const int&, const int&)>>
mapIntToString1(ReverseSort<int>);
mapIntToString(ReverseSort<int>)

mapIntToString1[1] = "one";
mapIntToString1[2] = "two";
mapIntToString1[3] = "three";
mapIntToString1[4] = "four";

for (auto m : mapIntToString1){
cout << m.first << "," << m.second << endl;
}
}

Victor Bazarov

unread,
Jul 13, 2015, 3:20:55 PM7/13/15
to
On 7/13/2015 2:39 PM, Doug Mika wrote:
> If a template variable type can be deduced from the variables
> provided in the constructor of a template class, then is it needed?

It can't. On what basis do you assume that it can? You ask a question
that is not possible to answer because it is based on a wrong premise.

> I thought it wasn't, but when in the following program (below) I
replace the line:
> map<int, string, function<bool(const int&, const int&)>>
> mapIntToString1(ReverseSort<int>);
> with:
> map<int, string>
> mapIntToString1(ReverseSort<int>);
> the code no longer compiles? Why isn't the compiler able to deduce
from the submitted function ReverseSort<int> that the template variable
type is bool(*)(const int&, const int&)?

It's not required to, by the language Standard. And the requirement is
absent because it would be much more difficult to define other rules if
it existed.

> Is the compiler only smart
enough to deduce this for template functions?

Yes, the compiler is obviously dumber than you.

The rules say that to define an object of some type you need to provide
the type. Deduction of template arguments is only allowed by the
Standard in some contexts, and the constructor arguments for direct
initialization are not one of those contexts. A constructor is not a
regular function.

Find a copy of a good book that explains it and study. A decent book
enumerates the rules and shows what rules exist. A great book also
explains why such rules exist. The former is often enough, the latter
is sometimes too difficult to comprehend.

> [..]

V
--
I do not respond to top-posted replies, please don't ask

Alf P. Steinbach

unread,
Jul 13, 2015, 4:12:13 PM7/13/15
to
On 13-Jul-15 8:39 PM, Doug Mika wrote:
> If a template variable type can be deduced from the variables provided in the
> constructor of a template class, then is it needed? I thought it wasn't, but
> when in the following program (below) I replace the line:
>
> map<int, string, function<bool(const int&, const int&)>>
> mapIntToString1(ReverseSort<int>);
>
> with:
>
> map<int, string>
> mapIntToString1(ReverseSort<int>);
>
> the code no longer compiles? Why isn't the compiler able to deduce from the
> submitted function ReverseSort<int> that the template variable type is
> bool(*)(const int&, const int&)? Is the compiler only smart enough to deduce
> this for template functions?

The language was not designed to support this, but the need has become
more and more manifest, with all kinds of "make" functions being defined.

There is some discussion at SO, at

<url:
http://stackoverflow.com/questions/29677505/why-cant-constructors-deduce-template-arguments>

There is a current proposal to support this, N4471, referenced from the
SO discussion,

<url: http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4471.html>

and it lists some of the difficulties, in particular that

* The constructor argument type can be indirectly defined in terms of
a template parameter, which then is difficult to deduce.

I'm not sure I agree with the comment in the "inject class name" example
though.

This looks like still a pretty rough draft that needs to be ironed out.
I can't see that it will make C++17. But never say never! :)


Cheers & hth.,

- Alf

--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
Message has been deleted
0 new messages