-2

I have to return an int and a function object to the caller, I was thought of returning a tuple like make_tuple(int,[](some){}) I am now on GCC that doesn't support decltpe(auto) as a return type, is ther any way i could make my return type as std::tuple<int,auto> myfun() right now I've been doing like below(but not sure)

auto myfun()->decltype(make_tuple(100,p))
{
   auto p=[](some){};
   return make_tuple(100,p);
}

what I am doing is ok?

RaGa__M
  • 2,550
  • 1
  • 23
  • 44
  • 3
    Does this compile? It shouldn't. – NathanOliver Aug 17 '16 at 19:29
  • Why do you need `decltype(auto)` here? If your compiler supports return type deduction for functions, then getting rid of the trailing return type in the example above should work. – Praetorian Aug 17 '16 at 19:31
  • 3
    So it's quicker to ask a question here than create a simple testcase and try compiling that? – Praetorian Aug 17 '16 at 19:32
  • 2
    @FallingFromBed You can always create a [mcve] and compile that to test things. That would have told you it would not compile. – NathanOliver Aug 17 '16 at 19:32
  • @FallingFromBed what cpu do you have? Compiling this on my computer takes a fraction of a second. Hardly unbearable. – eerorika Aug 17 '16 at 19:35
  • "I am now on GCC that doesn't support `decltpe(auto)` [sic] as a return type" False. It does. Did you downgrade to some ancient version? Anyway, that's not why this code can't compile, as simple testing would've shown. One need not ask "scholars" to predict what a 4-line program will do. – underscore_d Aug 17 '16 at 19:38
  • Best practice is to make an effort on your own before demanding help. If it's not previously answered, do a self-answering Q&A afterwards. – jonspaceharper Aug 17 '16 at 21:12

1 Answers1

5

No, since p is not available at that scope, and a lambda expression shall not appear in unevaluated context (which is decltype). (See more here)

You can, however, wrap it in an std::function:

std::tuple<int, std::function<void(some)>> myfun()
{
  std::function<void(some)> p = [](some){};
  return std::forward_as_tuple(100, p);
}
Community
  • 1
  • 1
erenon
  • 18,838
  • 2
  • 61
  • 93
  • 2
    `std::funciton` adds indirection where none is originally required. I do not like this. – SergeyA Aug 17 '16 at 19:35
  • @SergeyA You cannot return a type that you don't know. I can't see a better way without putting the type outside the function. – NathanOliver Aug 17 '16 at 19:40
  • 1
    same remark as from @SergeyA. If there are no variables captured, I'd use a function pointer instead of std::function (--no indirection through type erasure)... or better avoid the problem and define `p` outside of the function. – davidhigh Aug 17 '16 at 19:41
  • @davidhigh: A function pointer is just as bad as `std::function` (no inlining possible). If performance is really that critical, the callable type should be defined outside the function. – erenon Aug 17 '16 at 19:49
  • @NathanOliver, neitheir do I - or I'd provide one in the answer. I still can dislike this one, can't i? – SergeyA Aug 17 '16 at 19:49
  • @SergeyA Sure you can. I think I overreacted and interpreted disliked as down vote which is not the case. I'll go back to pondering a solution. – NathanOliver Aug 17 '16 at 19:51
  • @erenon: I meant I'd use it if I really need it hard and dirty :-). In this case, see [here](http://stackoverflow.com/questions/5057382/what-is-the-performance-overhead-of-stdfunction) and [here](http://stackoverflow.com/questions/14306497/performance-of-stdfunction-compared-to-raw-function-pointer-and-void-this), for example, for some not too definite answers – davidhigh Aug 17 '16 at 19:55