🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Wojciech Muła Thu, 06 Oct 2016 17:00
[c++] strchr jako argument templata się nie kompiluje
#308826
Author: Wojciech Muła
Date: Thu, 06 Oct 2016 17:00
38 lines
2302 bytes
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?

w.
Re: [c++] strchr jako argument templata się nie kompiluje
#308827
Author: bartekltg
Date: Thu, 06 Oct 2016 17:45
59 lines
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
Re: [c++] strchr jako argument templata się nie kompiluje
#308829
Author: Wojciech Muła
Date: Fri, 07 Oct 2016 17:43
61 lines
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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads