Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

State or no state, that is the question

30 views
Skip to first unread message

Daniel

unread,
Dec 4, 2016, 11:08:07 PM12/4/16
to
Consider a class


template <class Allocator>
struct A_base
{
A_base(const Allocator& allocator)
: allocator_(allocator)
{
}

Allocator allocator_;
};


template <class Allocator>
struct C : public A_base<Allocator>
{
C()
: A_base(Allocator())
{
}
C(const Allocator& allocator)
: A_base{ allocator }
{

}

void f()
{
}
}

Is there a reliable way within function f to determine whether allocator_ is stateful or stateless?

I thought about using std::is_default_constructible<Allocator>, as presumably a stateful allocator would not have a default constructor.

Or testing whether sizeof(C) == sizeof the same data structure except not inheriting from A_base.

Any thoughts?

Thanks,
Daniel

Christopher J. Pisz

unread,
Dec 5, 2016, 1:13:33 AM12/5/16
to
I'd agree with you that sizeof(C) == sizeof the same data structure
except not inheriting from A_base is pretty much fullproof, but I am no
expert on allocators.

Öö Tiib

unread,
Dec 5, 2016, 3:22:23 AM12/5/16
to
On Monday, 5 December 2016 06:08:07 UTC+2, Daniel wrote:
> Is there a reliable way within function f to determine whether allocator_ is stateful or stateless?

Take from Boost.TypeTraits 'is_stateless'. Docs of TypeTraits:
http://www.boost.org/doc/libs/1_62_0/libs/type_traits/doc/html/index.html

>
> I thought about using std::is_default_constructible<Allocator>, as presumably a stateful allocator would not have a default constructor.
>
> Or testing whether sizeof(C) == sizeof the same data structure except not inheriting from A_base.
>
> Any thoughts?

Boost docs say that "is_stateless" is implemented like that:

::boost::has_trivial_constructor<T>::value
&& ::boost::has_trivial_copy<T>::value
&& ::boost::has_trivial_destructor<T>::value
&& ::boost::is_class<T>::value
&& ::boost::is_empty<T>::value

Daniel

unread,
Dec 5, 2016, 11:50:35 AM12/5/16
to
Thanks! And it looks like all the pieces are in <type_traits>.

Daniel

Daniel

unread,
Dec 5, 2016, 9:39:30 PM12/5/16
to
On Monday, December 5, 2016 at 3:22:23 AM UTC-5, Öö Tiib wrote:
>
> Boost docs say that "is_stateless" is implemented like that:
>
> ::boost::has_trivial_constructor<T>::value
> && ::boost::has_trivial_copy<T>::value
> && ::boost::has_trivial_destructor<T>::value
> && ::boost::is_class<T>::value
> && ::boost::is_empty<T>::value

boost::is_stateless<std::allocator<char>>::value turns out to return false.
boost::has_trivial_constructor<T>::value and boost::has_trivial_copy<T>::value
both evaluate to false (the other parts evaluate to true.)

For my purposes, I'm tentatively using

template <typename T>
struct is_stateless_allocator
: public std::integral_constant<bool,
(std::is_default_constructible<T>::value &&
std::is_empty<T>::value)>
{};

If anyone has any suggestions for other conditions to throw in there, please
let me know.

Thanks,
Daniel


0 new messages