Indiana Unversity logo[ConceptGCC]

ConceptGCC :

Re: Associated type as return type.

From: Christopher Eltschka (celtschk_at_[hidden])
Date: 2008-08-19 08:53:43


> -----Ursprüngliche Nachricht-----
> Von: "hirohito none" <hitobasira_at_[hidden]>
> Gesendet: 18.08.08 20:19:26
> An: conceptgcc_at_[hidden]
> Betreff: [ConceptGCC] Associated type as return type.

> Following code can't be compiled with ConceptGCC 4.3.0 Alpha 7
>
> #include <concepts>
> auto concept Foo < typename T >
> {
> requires std::CopyConstructible< T > ;
> typename result_type ;
> requires std::CopyAssignable< result_type, int > ;
> result_type & value( T & x ) { return x.value ; }
> }
>
> template < typename T > requires Foo< T >
> void f( T x )
> { value(x) = 0 ; }
>
> struct Bar
> { int value ; } ;
>
> int main()
> { f(Bar()) ; }

How should the compiler deduce Foo<Bar>::return_type?
There are many types to which an int can be assigned:
All builtin numeric types (both integer and floating point),
bool, any user-defined type with a non-explicit constructor
taking an int (or any builtin type which implicitly converts
to an int), any user-defined type which implements
operator= taking an int, or taking anything implicitly
convertible to an int.

The fact that the default implementation happens
not to compile with result_type different from the
type of T::value shouldn't matter, because that default
implementation might not be instantiated at all if the
type in question supplies its own value function.

I think it should work if you give the result_type a default
as well. If ConceptGCC implements the C++0x decltype
(I'm not sure about that), you could extend the result_type
definition in concept Foo to

typename result_type = decltype(T::value);

If ConceptGCC doesn't implement decltype, you can
use gcc's non-standard typeof instead.