Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

volatile and const volatile std::tuple and std::get

Tags:

c++

c++11

Looking at the C++11 standard. I can see that specialization of std::tuple_size and std::tuple_element are provided for volatile and const volatile tuple.

template <size_t I, class T> class tuple_element<I, volatile T>;
template <size_t I, class T> class tuple_element<I, const volatile T>;

template <class T> class tuple_size<volatile T>;
template <class T> class tuple_size<const volatile T>;

But std::get do no offer specialization for volatile or const volatile tuple.

I tried the following code on GCC.4.8.1

volatile std::tuple<int, int> a(1, 1);
std::cout << "a<0>=" << std::get<0>(a) << "\n";

I get error: no matching function for call to 'get(volatile std::tuple<int, int>&)'

So if I understand I can create (const) volatile tuples but I can not access their elements.

Is this an expected behavior or an oversight ?

Thanks very much.

like image 870
user4237465 Avatar asked Nov 10 '14 22:11

user4237465


1 Answers

This is not only the case for std::get but also for the relational operators or swap. Why does swap not support volatile tuples then? Because no move constructor of tuple takes volatile tuples. Same goes for the assignment operator. Actually, when considering the standard library in its entirety, almost no class or template provides overloads for volatile objects1. Maybe it would be to much of a hassle in standardization and implementation; Or perhaps it's was regarded nonsensical to have volatile class objects. In either case, volatile tuples are currently quite unusable and adding a get overload for them would, with respect to the current state of the interface, be inconsistent.

Using volatile tuple as a type (and not an object) is not per se problematic and could be useful. That, and the fact that pretty much every single other type trait in the standard library is also specialized for all cv-qualifiers, lead to tuple_element and tuple_size supporting it.


1 One can easily check this by searching for volatile in the C++ standard from clause 17 on. One will find that no function (template) apart from the ones for atomics in clause 29 is overloaded for volatile parameters.

like image 146
Columbo Avatar answered Oct 05 '22 20:10

Columbo