1

Can we not do anything like this?

auto f(int n, char *arr[]) -> decltype(char *[n]) /* doesn't work */ {
    char *tmp[n]; // RSA, the size depends on argc
    for (int i = 0; i < n; ++i) tmp[i] = arr[i];
    return tmp;
}

int main(int argc, char *argv[]) {
    auto arr = f(argc, argv);
    for (auto e : arr)
        cout << e << endl;
}

I love value semantics (as long as my compiler can do proper inlining) especially when I refactor a large function into a set of smaller utilities.

Edit: oops, it didn't make it into C++14... (Changed the title from "C++14" to "C++1z(?)".) How about "Array Extensions TS"? Does it support anything like this?

2
  • C++14 doesn't have runtime sized arrays. There are implementations of string_view and array_view that you could use to wrap the parameters, but those aren't standard yet either.
    – chris
    Nov 12, 2015 at 3:21
  • 1
    Built-in arrays have been abandoned by new C++ language development because they suck. You must either use std::array or std::vector or similar to take advantage of such wonderful features as returning an array.
    – Neil Kirk
    Nov 12, 2015 at 3:28

2 Answers 2

3

The canonical C++ way to do what you want to do is to use a std::vector (or std::array if the number of elements is known at compile time). C++ does not use VLAs, as noted in M.M's answer.

If the character pointers involved are not going to be dangling (e.g., in your argv case), you can do this:

std::vector<char*> f(int n, char** arr)
{
    std::vector<char*> tmp;
    tmp.reserve(n);
    for (int i = 0; i < n; ++i)
        tmp.push_back(arr[i]);
    return tmp;
}

If the pointers will dangle, it's better to create std::string objects:

std::vector<std::string> f(int n, char** arr)
{
    std::vector<std::string> tmp;
    tmp.reserve(n);
    for (int i = 0; i < n; ++i)
        tmp.emplace_back(arr[i]);
    return tmp;
}

(emplace_back is new to C++11. In older C++ versions, you can just use push_back.)

5
  • vector<char *> would be more akin to OP's code, which doesn't copy the string contents. (Unclear which version suits his needs better of course)
    – M.M
    Nov 12, 2015 at 3:28
  • @M.M My original answer did use vector<char*>, but I didn't want to risk any danglingness, so.... (Granted, a non-problem for argv, but in general.) Nov 12, 2015 at 3:28
  • Even in C, which has VLAs, you cannot return arrays from functions (and in particular you cannot return VLAs).
    – Kerrek SB
    Nov 12, 2015 at 3:35
  • char* vs string or whatever is irrelevant to the main topic. In fact my real use case has nothing to do with argc / argv.
    – nodakai
    Nov 12, 2015 at 3:38
  • 1
    @nodakai Great! Then just use the appropriate vector type and go from there. It's the common way to do variable-sized-array-like things in C++. Nov 12, 2015 at 3:39
2

VLAs are not permitted in any version of ISO C++ .

Some compilers offer VLA as an extension. You will need to consult the documentation for your compiler to see details about the nature of the extension.

It seems unlikely to me that returning a VLA would be allowed by any extension (or by C++17 if VLAs make it in), since returning an array (even a non-VLA) does not make sense.

There is no syntax that specifies returning an array by value; in the line return tmp;, tmp decays to a pointer. Arrays do not have value semantics.

1
  • Tagging your compiler might help, although posting on mailing list for your compiler might be the best option to get a definitive answer about whether the compiler allows returning a VLA
    – M.M
    Nov 12, 2015 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.