0

How could i select the overloaded function that i mean to call ?

Consider this code:

void  foo (std::vector<int> const &variable);
void  foo (std::vector<int> variable);

For example in above code i want to call void foo (std::vector<int>);, I tried :

void bar ()
{
  std::vector<int> tmp;

  foo(tmp);
  foo(static_cast<std::vector<int>(tmp));
  foo(boost::implicit_cast<std::vector<int>>(tmp));
  foo((std::vector<int>)tmp);
  foo(std::vector<int>(tmp));
}

But it's failed, How could i do that without changing the functions signature ?

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
  • why would you need both? If needed you can create a copy from the `const` reference and If you don't need the copy why have the overload that takes the vector by value? – 463035818_is_not_an_ai Nov 21 '20 at 14:27
  • 3
    If you really do need both, could you `static_cast` the functions like `static_cast)>(foo)` and `static_cast const&)>(foo)`? – Lily Nov 21 '20 at 14:31
  • @idclev463035818, I don't need it in a real program, I'm just eager to know if it is possible. – Ghasem Ramezani Nov 21 '20 at 14:31
  • I'm fairly sure no possible argument type will help in an ordinary named function call `foo(anything)`. The only way to use `foo` would be to convert the name `foo` to a specific reference-to-function or pointer-to-function type, as @ColonD suggests. – aschepler Nov 21 '20 at 14:35
  • @ColonD, It's work, thank you. – Ghasem Ramezani Nov 21 '20 at 14:35
  • `void (*fun)(std::vector const &variable) = foo; fun1(someVector);` seem to work in gcc. The cast suggested above looks cleaner, though. – chi Nov 21 '20 at 14:35
  • @idclev463035818 Why is the second dupe different than the final dupe? They seem to be the same. – cigien Nov 21 '20 at 14:40
  • @idclev463035818 Hmm, I see your point. I think a question asking *why* it's ambiguous can be answered with *how* to disambaguate though. It might take a little modification of the answer, but you could also just show *how* and link to another answer that explains the *why*. – cigien Nov 21 '20 at 14:43
  • I wonder why my question marked as *duplicate* ? If it's a duplicate question why i couldn't find my answer (like answer @idclev463035818 and @ColonD) ? That posts just explain why it's ambiguous and how to solved with another way. – Ghasem Ramezani Nov 21 '20 at 14:46
  • That's fair. But the answer that you want can also be written on the 3rd target in the list. As pointed out though, the solutions in the comments are "hacks" and you shouldn't do that anyway. – cigien Nov 21 '20 at 14:48
  • @idclev463035818 Your point about the "don't do it disclaimer" is good. Hope you get around to writing that answer on the target :) – cigien Nov 21 '20 at 14:50
  • here you go: https://stackoverflow.com/a/64944404/4117728 – 463035818_is_not_an_ai Nov 21 '20 at 14:52
  • 2
    if you think none of the duplicates answers your question you need to do more than listing them and saying that they dont answer your quesiton. You need to explain why they don't apply in your case. Arent you fine with the `static_cast` propsed in an answer to this question https://stackoverflow.com/questions/5465293/function-overloading-based-on-value-vs-const-reference ? – 463035818_is_not_an_ai Nov 21 '20 at 14:56
  • @idclev463035818, You right i don't looked carefully, it's my bad sorry. – Ghasem Ramezani Nov 21 '20 at 17:00
  • @GhasemRamezani no problem. I posted the answer just shortly before your edit, but consider that in general getting closed as duplicate is not the end of the story. If you explain why your question is different than the others it can be reopened, but then you should clarify *why* it is different. Missing the answer on a similar question, doesn't make the question a different question ;) – 463035818_is_not_an_ai Nov 21 '20 at 17:11

0 Answers0