C++ perfect forward copy-only types to make_tuple -


i'm playing little snippet:

#include <tuple>  struct copy_only {     copy_only() = default;     copy_only(copy_only&&) = delete;     copy_only(const copy_only&) = default; };  template <typename ...ts> void foo(ts&& ...xs) {     auto t = std::make_tuple(std::forward<ts>(xs)...);     (void) t; }  int main() {    foo(copy_only{}); } 

it compiles fine gcc7 , clang3.6, clang3.7, clang3.8 (wandbox), , clang8.0 (macos sierra). doesn't compile clang3.9, g++6.2 (macos sierra) nor clang4.0 (wandbox). of them complain deleted move constructor.

it works fine move-only types. @ least on above compilers available on wandbox.

is code example of correct way of generic perfect forwarding tuple in c++14?

    auto t = std::make_tuple(std::forward<ts>(xs)...); 

this indeed correct way of forwarding arguments tuple.

the compile errors caused explicitly declaring copy_only's move constructor deleted. normally, if declared copy constructor, it'd omitted , in move contexts copy constructor chosen - has since c++98. because explicitly declared it, participate in overload resolution , causes code ill-formed if selected.

here's helpful chart courtesy of howard hinannt: chart

this can solved removing offending line class definition:

struct copy_only {     copy_only() = default;     //copy_only(copy_only&&) = delete;     copy_only(const copy_only&) = default; }; 

now, whether code should compile or not: far can tell, should. tuple's move constructor defined as:

tuple(tuple&& u) = default;

requires: is_move_constructible<ti>::value true i.

since copy_only not move constructible, shouldn't declared , shouldn't participate in overload resolution.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -