Topic: c++0x: concepts and model checking


Author: german diago <germandiago@gmail.com>
Date: Wed, 17 Jun 2009 20:10:22 CST
Raw View
Hello. Will there be a way to check if a type conforms to a concept?

I would like to mark some types as interfaces, like this:

class MyInterface {
  void myfunc() = 0;
};

concept InterFaceType<MyInterface> {}

I think this is enough to know if a type models a concept, but, do I
need an additional trait like this in c++0x?

template <class T>
struct isInterfaceType : public false_type {};

template <> struct isInterfaceType<MyInterface> : public true_type {};

Thanks for your time.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Thu, 18 Jun 2009 18:19:06 CST
Raw View
On 18 Jun., 04:10, german diago <germandi...@gmail.com> wrote:
> Hello. Will there be a way to check if a type conforms to a concept?
>
> I would like to mark some types as interfaces, like this:
>
> class MyInterface {
>   void myfunc() = 0;
> };
>
> concept InterFaceType<MyInterface> {}
>
> I think this is enough to know if a type models a concept, but, do I
> need an additional trait like this in c++0x?
>
> template <class T>
> struct isInterfaceType : public false_type {};
>
> template <> struct isInterfaceType<MyInterface> : public true_type {};

It is not obvious to me that this would be useful. Where and how would
you use this trait?

Cheers!
SG


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: restor <akrzemi1@gmail.com>
Date: Thu, 18 Jun 2009 18:18:11 CST
Raw View
On 18 June, 04:10, german diago <germandi...@gmail.com> wrote:
> Hello. Will there be a way to check if a type conforms to a concept?
>
> I would like to mark some types as interfaces, like this:
>
> class MyInterface {
>   void myfunc() = 0;
>
> };
>
> concept InterFaceType<MyInterface> {}
>
> I think this is enough to know if a type models a concept, but, do I
> need an additional trait like this in c++0x?
>
> template <class T>
> struct isInterfaceType : public false_type {};
>
> template <> struct isInterfaceType<MyInterface> : public true_type {};

Hi, I am not sure what you want to achieve, but perhaps a simple type
trait

template <class T>
struct std::is_abstract;

would do?
Regards,
&rzej


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Thu, 18 Jun 2009 18:23:43 CST
Raw View
On 18 juin, 04:10, german diago <germandi...@gmail.com> wrote:
> Hello. Will there be a way to check if a type conforms to a concept?

 From the rest of your message, you want a facility to check whether a
type conforms to a concept as a meta-function (similarly to
type_traits).

I don't think that's planned in the standard.
Indeed, it would require a macro to generate the appropriate meta-
function from a concept, due to lack of concept template parameters.

However, the point of concepts is that you shouldn't need meta-
functions as much anymore.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: german diago <germandiago@gmail.com>
Date: Fri, 19 Jun 2009 23:53:03 CST
Raw View
Yes. I want to achieve that. But I don't understand why I have to
write a trait
if the compiler knows (via concept maps) the trait anyway.

I mean if I have a pointer, which maps to concept Pointer, why do I
have to write
an is_pointer trait. The compiler knows this anyway.


> Hi, I am not sure what you want to achieve, but perhaps a simple type
> trait
>
> template <class T>
> struct std::is_abstract;
>
  ]



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Sat, 20 Jun 2009 12:39:47 CST
Raw View
On 20 Jun., 07:53, german diago <germandi...@gmail.com> wrote:

> Yes. I want to achieve that. But I don't understand why I have
> to write a trait if the compiler knows (via concept maps) the
> trait anyway.
>
> I mean if I have a pointer, which maps to concept Pointer, why
> do I have to write an is_pointer trait.

Why would you? Either you already know the type you're dealing with
and that it models (or doesn't model) this Pointer concept or you're
inside a template where this type depends on template parameters. The
latter case calls for requires clauses and/or concept-based
overloading.

Can you give any example where this would be really useful? I still
fail to see good use cases.

Some time ago I was basically suggesting the same thing. Something
like this:

   static_assert(ForwardIterator<list<int>::iterator>, "WTF!?");

But really, what problem does it solve? In non-template contexts the
compiler can already apply thorough type checking. Semantic
constraints can hardly be checked via concepts, either.

Cheers!
SG

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Sat, 20 Jun 2009 12:39:06 CST
Raw View
On 20 juin, 07:53, german diago <germandi...@gmail.com> wrote:

> I mean if I have a pointer, which maps to concept Pointer, why do I
> have to write
> an is_pointer trait. The compiler knows this anyway.

As I said, you can generate the trait from the concept.

Here is a trivial macro that does that:

#include <tr1/type_traits>

#define TRAIT_CONCEPT_DEF(concept)
\
template<typename T>
\
struct is_##concept : std::false_type
\
{                                                                      \
};
\

\
template<concept T>
\
struct is_##concept<T> : std::true_type
\
{                                                                      \
};

If you have a concept Pointer, calling TRAIT_CONCEPT_DEF(Pointer) will
generate a trait is_Pointer.

There is no way to do it more concisely AFAIK.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]