#Passing multiple string to a overloaded function with std::vector<std::string> argument.

20 messages · Page 1 of 1 (latest)

agile mortar
#

Hey guys,

Let's say I have those two functions:

void Foo(std::string String);
void Foo(std::vector<std::string> Strings);

How do I call the second one by creating the vector on function call?

I'm asking because sometime does Foo({"One", "Two"}) work and sometimes it can't find a matching function prototype. And (sometimes?) does Foo(("One", "Two")) work.

What is the correct way to call such a function?
How is this implicit object creation called?

And how would it look like if I would create the object explicitly (although I want to keep it short if possible)?
I.e. which braces would I need to use?
Foo(std::vector<std::string>{"One", "Two"}) ?
Or is there even another way?

Because I know that it is also (sometimes?) possible to create objects like this:
Object o = Object(1, 2);
And sometimes but now always like this:
Object o(1, 2);

So many possibilities and sometimes some of them don't work, although I don't see why, so I just check multiple syntaxes until the compiler is okay with it.

hoary veldtBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

woeful mist
#

What you've suggest is one option

#

Otherwise you could even do something like

Foo({std::string{"aa"}, "sb"});
#

But I'd probably go with Foo(std::vector<std::string>{"One", "Two"}), its clearer

#

A shorter way would even be
Foo({{"aa"}, {"sb"}}); but this is even more confusing to me

#

By the way,
Object o = Object(1, 2); Object o(1, 2);

Are the same thing to the compiler

agile mortar
#

Thanks so far. Honestly, I try to omit something like std::vector<std::string> because this makes reading code kinda difficult, especially, if there is more than 1 function argument.

agile mortar
woeful mist
#

This:

struct Fo {
  Fo(int x) {}
};

int main()
{
    Fo f(5);
}
``` is compiled as:
```cpp
struct Fo
{
  inline Fo(int x)
  {
  }
  
};



int main()
{
  Fo f = Fo(5);
  return 0;
}
``` (=> cppinsights)
agile mortar
#

Also, would be really nice to understand the differences of curly braces and parenthesis when creating an object and passing arguments to the constructor, because even there sometimes works the on, and sometimes the other

woeful mist
#

You've probably experienced "most vexing parse"

agile mortar
#

But it probably depends on how the class is constructed

agile mortar
woeful mist
#

But sometimes you don't want {} because some constructors (think of std::vector) will specifically be used if you use {}

#
std::vector<int> x{2,3}; // which constructor is called?
hoary veldtBOT
#

@agile mortar Has your question been resolved? If so, run !solved :)

agile mortar
#

Although I'm open for other input, because it seems like I will still listen to the compiler instead of knowing what I am doing:

#

!solved