Article View: pl.comp.lang.c
Article #308829Re: [c++] strchr jako argument templata się nie kompiluje
From: Wojciech Muła
Date: Fri, 07 Oct 2016 17:43
Date: Fri, 07 Oct 2016 17:43
61 lines
3255 bytes
3255 bytes
On Thursday, October 6, 2016 at 7:45:41 PM UTC+2, bartekltg wrote:
> On 06.10.2016 19:00, Wojciech Muła wrote:
> > GCC 4.9.2, taki kod:
> >
> > ---
> > #include <cstring>
> >
> > const char* my_strchr(const char* s, int c) {
> > return s + c;
> > }
> >
> > template <typename STRLEN>
> > const char* foo(STRLEN strchr_fun, const char* s, int c) {
> > return strchr_fun(s, c);
> > }
> >
> > int main() {
> > foo(my_strchr, "hello world", '?');
> > foo(std::strchr, "hello world", '?');
> > }
> > ---
> >
> > $ gcc -std=c++14 bug.cpp
> >
> > bug.cpp: In function ‘int main()’:
> > bug.cpp:14:40: error: no matching function for call to ‘foo(<unresolved overloaded function type>, const char [12], char)’
> > foo(std::strchr, "hello world", '?');
> > ^
> > bug.cpp:14:40: note: candidate is:
> > bug.cpp:8:13: note: template<class STRLEN> const char* foo(STRLEN, const char*, int)
> > const char* foo(STRLEN strchr_fun, const char* s, int c) {
> > ^
> > bug.cpp:8:13: note: template argument deduction/substitution failed:
> > bug.cpp:14:40: note: couldn't deduce template parameter ‘STRLEN’
> > foo(std::strchr, "hello world", '?');
> >
> > Po prostu nie rozumiem. Ten strchr to jest jakieś makro,
> > czy co?
>
>
> A który strchr?
> Bo sa dwa:
>
> const char * strchr ( const char * str, int character );
> char * strchr ( char * str, int character );
OK, nie zauważyłem tego.
> Wewnątrz używasz go na const, ale w momencie, gdy kompilator
> decyduje, co jest argumentem foo(), nie wie tego.
Dzięki wielki! Teraz rozumiem, chociaż nie akceptuję.
> foo<const char *(const char *,int)>(std::strchr, "hello world", '?');
>
> Się kompiluje, ale brzydkie.
> Pewnie da się z jakimś decltype zrobiuć ładniej ;-)
Zrobię przez lambdę, jak człowiek. :)
w.
Technical Details
Message-ID:
<b1406c6e-761d-4d8e-ae3d-f1b3592c1065@googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!news.glorb.com!l13no967312itl.0!news-out.google.com!w143ni2892itb.0!nntp.google.com!l13no967302itl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailReferences:
<355b5d0e-c1fe-431d-bf40-adf894da42b0@googlegroups.com> <nt62k4$8ib$1@node1.news.atman.pl>Show Full Headers
Complete Article Headers
X-Received: by 10.107.187.3 with SMTP id l3mr5660156iof.38.1475862187483; Fri, 07 Oct 2016 10:43:07 -0700 (PDT) X-Received: by 10.36.137.67 with SMTP id s64mr2422577itd.1.1475862187441; Fri, 07 Oct 2016 10:43:07 -0700 (PDT) Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!news.glorb.com!l13no967312itl.0!news-out.google.com!w143ni2892itb.0!nntp.google.com!l13no967302itl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: pl.comp.lang.c Date: Fri, 7 Oct 2016 10:43:06 -0700 (PDT) In-Reply-To: <nt62k4$8ib$1@node1.news.atman.pl> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=37.47.132.253; posting-account=VFwkXwoAAADdT4-lLKRZrMYkTjizGoyn NNTP-Posting-Host: 37.47.132.253 References: <355b5d0e-c1fe-431d-bf40-adf894da42b0@googlegroups.com> <nt62k4$8ib$1@node1.news.atman.pl> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <b1406c6e-761d-4d8e-ae3d-f1b3592c1065@googlegroups.com> Subject: Re: [c++] strchr jako argument templata się nie kompiluje From: Wojciech Muła <wojciech_mula@poczta.onet.pl> Injection-Date: Fri, 07 Oct 2016 17:43:07 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit