3

So I have a program that looks something like this:

const char *Argv[] = {"stuff", "stuff1", "stuff3"};

bool pass = xxxxx::yyyyy(Argv.begin(), Argv.end(), Tri);

I think this is illegal because const char * is not a user-defined type. However, I am not sure how to fix this. Would I need to change the first line or the second? Or both?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
grizzleKat456
  • 127
  • 1
  • 7
  • BTW it's actually illegal because `(const char*)[3]` is not a user-defined (class) type. It's the arrayness that's tripping you up. The element type is irrelevant. – Asteroids With Wings Jun 01 '20 at 15:52
  • Just use the ptrs: `Argv`, and `Argv + 3` -- iterators are just generalization of ptrs in C++. – wcochran Jun 01 '20 at 16:32

2 Answers2

5

Argv is an array (of const char*s), and yes you can't call begin() and end() as your code showed, array doesn't have such member functions. Instead, you can use std::begin and std::end for it.

const char *Argv[] = {"stuff", "stuff1", "stuff3"};
bool pass = xxxxx::yyyyy(std::begin(Argv), std::end(Argv), Tri);

If you use other standard containers like std::vector or std::array instead, then you can call the member functions begin() and end() on them. Note that even for these containers you can still use std::begin and std::end on them, which have the same effect as calling their member function begin() and end().

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

Just for completeness, and to show the c++ way, you could change the first line to:

std::array<std::string_view, 3> Argv {"stuff", "stuff1", "stuff3"};

and then the second line:

bool pass = xxxxx::yyyyy(Argv.begin(), Argv.end(), Tri);

will work just fine.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • There doesn't seem to be any need for two layers of indirection and dynamic allocation here. An array of `const char*` is just fine (though I'd be tempted, at least, to put them in a `std::array`). – Asteroids With Wings Jun 01 '20 at 15:53
  • @AsteroidsWithWings I see your point, but is the performance in this piece of code really affected by using a `vector`? I mean, it might be, but I always recommend `vector` unless there's a reason otherwise. – cigien Jun 01 '20 at 15:56
  • 1
    No, but you call it "the C++ way", which doesn't seem quite fair when the original is completely valid C++ and perfectly reasonable in many circumstances. Falling back on a relatively expensive solution for absolutely no gain is not good programming. It all adds up. Example: the trivial apps you download to your phone now are 200MB, 300MB, when _they simply don't need to be_. If programmers were not so wasteful, we'd be able to enjoy amazing amounts of storage with our modern technology. Instead, we're squandering advances in hardware with lazy code. – Asteroids With Wings Jun 01 '20 at 15:57
  • The c++ way would be to use a vector. There's plenty of terrible c++ code that's *valid*. I'm not suggesting *falling back* on `vector`, but *starting* there. – cigien Jun 01 '20 at 15:59
  • And I'm suggesting starting with the appropriate tool for the job, no matter which language you're writing. An array is "the C++ way" too. – Asteroids With Wings Jun 01 '20 at 16:00
  • Only when the appropriate tool for the job is understandable. The OP is clearly confused about arrays having member functions, and you're suggesting their code is good because it performs well? I'm sorry, but I've seen too many recommendations for writing essentially c code, that start off with the rationale you've given. I'm not saying you're *wrong*, I'm saying that people should start with the easy (and yes, lazy) stuff first, and get to the other stuff later. – cigien Jun 01 '20 at 16:04
  • A more accurate substitute would be `std::array` instead. Using a vector of strings involves many dynamic allocations that aren't needed here. – François Andrieux Jun 01 '20 at 16:04
  • @FrançoisAndrieux Ok for the vector vs array bit, but with SSO, is `string_view` really going to matter here? – cigien Jun 01 '20 at 16:05
  • @cigien Maybe not much. But conceptually, `std::string_view` is more appropriate when you want to wrap a literal. And if anything, `std::string` is probably bigger than `std::string_view`, and `std::string` will still have to copy the data, iterating over it. – François Andrieux Jun 01 '20 at 16:08
  • Arrays are very understandable. Pointing out that copy/pasting a `.begin()`/`.end()` pattern doesn't apply to them doesn't really change that in any fundamental or meaningful way. Certainly, avoiding such a trivial knowledge transfer by claiming that you "should" use four dynamically allocated pieces of data to be "writing C++" is not a forwards step in education. – Asteroids With Wings Jun 02 '20 at 20:47
  • @AsteroidsWithWings Yeah, I'll agree with that. On reflection, I figured I should change the answer, so I'd already changed it. – cigien Jun 02 '20 at 21:50