Indiana Unversity logo[ConceptGCC]

ConceptGCC :

Re: Need some help -- can't find the error in this code.

From: Daniel Lincke (daniel.lincke_at_[hidden])
Date: 2007-09-19 09:26:34


Hi Tom,

thx for answering. However, I was not able to solve the problem. First
of all I changed all ()-operators to operator()(const source_type& x);
Requiring than F to be CopyConstructible or change 'test' to accept it's
F parameter as a reference didn't show any effect.

However I tried to rename the operator() . Now the concept function
provides an method 'apply' :

+++++++++++++++++++++++++++++++++++++++++++

#ifdef __GXX_CONCEPTS__
#include <concepts>
#endif

#include <stdlib.h>
#include <stdio.h> // file I/O streams
#include <iostream>

using namespace std;

#ifdef __GXX_CONCEPTS__
concept Function<class F> {
typename source_type;
typename target_type;

target_type apply (const source_type& x);
};
#endif

class Test_Function1 {
public:
typedef int source_type;
typedef int target_type;

int apply (const int& t) {
return (t*2);
}
};

#ifdef __GXX_CONCEPTS__
concept_map Function<Test_Function1> {
typedef Test_Function1::source_type source_type;
typedef Test_Function1::target_type target_type;

target_type apply (const source_type& x) {
Test_Function1 f;
return f.apply(x);
}
};
#endif

template<class F>
#ifdef __GXX_CONCEPTS__
requires Function<F>
#endif
void test (const F& f) {
for (int i=0; i<10; ++i)
{cerr << i << " -> " << f.apply(i) << endl;} #### LINE 65! #####
}

int main (int argc, char *argv[]) {
// Test_Function1 f1;
// test (f1);
}

+++++++++++++++++++++++++++++++++++++++++++

The error occuring now is again not understandable (at least not for me
;) ) :

/opt/conceptgcc-4.3/bin/conceptg++ -c -o main.o main.cpp
main.cpp: In function ‘void test(const F&)’:
main.cpp:65: error: ‘const struct F’ has no member named ‘apply’
make: *** [main.o] Fehler 1

Note: If I delete the line "requires Function<F>" everything works fine ...
Note furthermore: If I shorten the code above to the following version
the error also occurs:

+++++++++++++++++++++++++++++++++++++++++++

#ifdef __GXX_CONCEPTS__
#include <concepts>
#endif

#include <stdlib.h>
#include <stdio.h> // file I/O streams
#include <iostream>

using namespace std;

#ifdef __GXX_CONCEPTS__
concept Function<class F> {
typename source_type;
typename target_type;

target_type apply (const source_type& x);
};
#endif

template<class F>
#ifdef __GXX_CONCEPTS__
requires Function<F>
#endif
void test (const F& f) {
for (int i=0; i<10; ++i)
{cerr << i << " -> " << f.apply(i) << endl;} #### LINE 65! #####
}

int main (int argc, char *argv[]) {
}

+++++++++++++++++++++++++++++++++++++++++++

so it seems to be a very fundamental problem here ... what am I doing
wrong? :'(

Greets Daniel

Tom Honermann wrote:
> Hi Daniel,
>
> I'm not 100% sure about this answer, but here goes...
>
> When defining a concept, identifiers that do not explicity quantify
> type attributes (such as 'const' or '&') are defaulted to 'const
> type&'. So, in your 'Function<F>' concept, the use of 'source_type'
> and 'target_type' on line 19 will become:
>
> const target_type& operator()(const source_type& x);
>
> As far as I know, there is no way to enforce "copy by value" arguments
> when defining a concept - but there also seems to be no reason to do
> so since a 'const type&' will generally suffice - and provide better
> flexibility for defining concept_maps.
>
> Regardless, the use of 'const type&' should be ok for your test case.
>
> I don't really understand the error message for line 53. However, I
> wonder if this is just a poor message for another error. In your
> 'test' function, the 'f' parameter is specified as "copy by value",
> but the 'Function' concept does not specify that the type is copy
> constructible, so this use is not valid (I think). There are a couple
> of ways of fixing this:
>
> 1. Add another requires clause to 'test' that 'F' be CopyConstructible
> 2. Change 'test' to accept it's F parameter as a reference.
>
> Tom.