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


Groups > comp.lang.c++ > #37064

Re: template variables

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: template variables
Date 2015-07-13 15:20 -0400
Organization A noiseless patient Spider
Message-ID <mo12vi$jcg$1@dont-email.me> (permalink)
References <0fe31d7f-95b8-4218-9a01-1ef61d828bbd@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web