Article View: pl.comp.lang.c
Article #308827Re: [c++] strchr jako argument templata się nie kompiluje
From: bartekltg
Date: Thu, 06 Oct 2016 17:45
Date: Thu, 06 Oct 2016 17:45
59 lines
2787 bytes
2787 bytes
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 );
Wewnątrz używasz go na const, ale w momencie, gdy kompilator
decyduje, co jest argumentem foo(), nie wie tego.
IMHO coś podobnego jak to, że nie dobiera się po typie wyniku.
http://stackoverflow.com/questions/25871381/c-overloaded-function-as-template-argument
foo(my_strchr, "hello world", '?');
foo<const char *(const char *,int)>(std::strchr, "hello world", '?');
Się kompiluje, ale brzydkie.
Pewnie da się z jakimś decltype zrobiuć ładniej ;-)
pzdr
bartekltg
Technical Details
Message-ID:
<nt62k4$8ib$1@node1.news.atman.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mailReferences:
<355b5d0e-c1fe-431d-bf40-adf894da42b0@googlegroups.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail From: bartekltg <bartekltg@gmail.com> Newsgroups: pl.comp.lang.c Subject: Re: [c++] strchr jako argument templata się nie kompiluje Date: Thu, 6 Oct 2016 19:45:39 +0200 Organization: ATMAN - ATM S.A. Lines: 65 Message-ID: <nt62k4$8ib$1@node1.news.atman.pl> References: <355b5d0e-c1fe-431d-bf40-adf894da42b0@googlegroups.com> NNTP-Posting-Host: 89-70-119-159.dynamic.chello.pl Mime-Version: 1.0 X-Trace: node1.news.atman.pl 1475775940 8779 89.70.119.159 (6 Oct 2016 17:45:40 GMT) X-Complaints-To: usenet@atman.pl NNTP-Posting-Date: Thu, 6 Oct 2016 17:45:40 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <355b5d0e-c1fe-431d-bf40-adf894da42b0@googlegroups.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit