Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #37060 > unrolled thread

template variables

Started byDoug Mika <dougmmika@gmail.com>
First post2015-07-13 11:39 -0700
Last post2015-07-13 22:11 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  template variables Doug Mika <dougmmika@gmail.com> - 2015-07-13 11:39 -0700
    Re: template variables Victor Bazarov <v.bazarov@comcast.invalid> - 2015-07-13 15:20 -0400
    Re: template variables "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2015-07-13 22:11 +0200

#37060 — template variables

FromDoug Mika <dougmmika@gmail.com>
Date2015-07-13 11:39 -0700
Subjecttemplate variables
Message-ID<0fe31d7f-95b8-4218-9a01-1ef61d828bbd@googlegroups.com>
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; 
     } 
}

[toc] | [next] | [standalone]


#37064

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2015-07-13 15:20 -0400
Message-ID<mo12vi$jcg$1@dont-email.me>
In reply to#37060
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

[toc] | [prev] | [next] | [standalone]


#37065

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2015-07-13 22:11 +0200
Message-ID<mo15vm$vae$1@dont-email.me>
In reply to#37060
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web