Topic: c++0x: Aren't constexpr constructible types allowed in constexpr functions?
Author: german diago <germandiago@gmail.com>
Date: Sun, 19 Sep 2010 18:16:00 CST Raw View
>
> 2) I don't see the "more readable" argument if I'm required to write
>
Not for such a thing, but... look at this and get convinced:
http://www.boostcon.com/community/wiki/show/private/2010/
Look at the slides for "Instantiations Must Go!" and "Instantiations
Must Go (2)!
--
[ 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: Wed, 15 Sep 2010 14:09:39 CST Raw View
Hello. I would like to know if I can do this in c++0x:
#include <type_traits>
using namespace std;
namespace std {
template <class T>
struct Type {
typedef T type;
};
template <class T>
constexpr bool isSerializable(Type<T>) { return false; }
}
namespace mine {
struct MyType {
int a_;
double b_;
};
constexpr bool isSerializable(Type<MyType>) { return true; }
}
If that were allowed, you wouldn't need (I think) to overload
namespace std for template partial specializations anymore, This
technique would use overloading to select the trait,
and, at the same time, client code would be more readable. Type traits
could be implemented like that as well
(although I know it's late for that).
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 15 Sep 2010 17:25:23 CST Raw View
On 15 Sep., 22:09, german diago <germandi...@gmail.com> wrote:
> Hello. I would like to know if I can do this in c++0x:
>
> #include <type_traits>
>
> using namespace std;
>
> namespace std {
>
> =A0 =A0 =A0 =A0template <class T>
> =A0 =A0 =A0 =A0struct Type {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0typedef T type;
> =A0 =A0 =A0 =A0};
>
> =A0 =A0 =A0 =A0template <class T>
> =A0 =A0 =A0 =A0constexpr bool isSerializable(Type<T>) { return false; }
>
> }
>
> namespace mine {
>
> struct MyType {
> =A0 =A0 =A0 =A0int a_;
> =A0 =A0 =A0 =A0double b_;
>
> };
>
> constexpr bool isSerializable(Type<MyType>) { return true; }
>
> }
You can do that, yes.
> If that were allowed, you wouldn't need (I think) to overload
> namespace std for template partial specializations anymore, This
> technique would use overloading to select the trait,
> and, at the same time, client code would be more readable. Type traits
> could be implemented like that as well
> (although I know it's late for that).
There are many good use-cases for constexpr functions, but
your example could not convince me yet ;-)
1) What is wrong by specializing a class template? In fact,
the process of class template specialization is much less
affected by subtle changes in meaning due to name lookup
issues.
2) I don't see the "more readable" argument if I'm required to write
static_assert(is_serializable(Type<mine::MyType>{}), "Ouch");
instead of
static_assert(is_serializable<mine::MyType>{}(), "Ouch");
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]