Monday 15 July 2013

c++ - error: use of deleted function. Why? -



c++ - error: use of deleted function. Why? -

i trying create function applies arbitrary functor f every element of provided tuple:

#include <functional> #include <tuple> // apply functor every element of tuple namespace detail { template <std::size_t i, typename tuple, typename f> typename std::enable_if<i != std::tuple_size<tuple>::value>::type foreachtupleimpl(tuple& t, f& f) { f(std::get<i>(t)); foreachtupleimpl<i+1>(t, f); } template <std::size_t i, typename tuple, typename f> typename std::enable_if<i == std::tuple_size<tuple>::value>::type foreachtupleimpl(tuple& t, f& f) { } } template <typename tuple, typename f> void foreachtuple(tuple& t, f& f) { detail::foreachtupleimpl<0>(t, f); } struct { a() : a(0) {} a(a& a) = delete; a(const a& a) = delete; int a; }; int main() { // create tuple of types , initialise them zeros using t = std::tuple<a, a, a>; t t; // creator simple function object increments objects fellow member struct f { void operator()(a& a) const { a.a++; } } f; // if works should end tuple of a's members equal 1 foreachtuple(t, f); homecoming 0; }

live code example: http://ideone.com/b8nlcy

i don't want create copies of a because might expensive (obviously in illustration not) deleted re-create constructor. when run above programme get:

/usr/include/c++/4.8/tuple:134:25: error: utilize of deleted function ‘a::a(const a&)’ : _m_head_impl(__h) { }

i know constructor deleted (that intentional) don't understand why trying create copies of struct. why happening, , how can accomplish without copying a?

this problem you're getting "deleted constructor" error for:

std::function<void(a)> f = [](a& a) { a.a++; };

you're trying set std::function passes a value. a, having no copy-constructor, can't passed value.

try matching actual argument type more carefully:

std::function<void(a&)> f = [](a& a) { a.a++; };

but since aren't capturing variables, can try

void(*f)(a&) = [](a& a) { a.a++; };

you've got major problem base of operations case of template recursion: if enable_if working, seems not be, you'll have ambiguous call. think need disable main case.

c++ c++11 gcc4.8

No comments:

Post a Comment