-1

I know C++ doesn't get well macro-functions, but I found this.

#define show(array)              \
    for (auto& x : (array))      \
    std::cout << x << std::endl; \

We can't have inline function alternative to this macro, because array is sent to function as a pointer, so it's impossible. With this, even if we try to apply something indecent, it will just result in compile-time error (if object isn't iterable), and it's obvious one:

'begin' was not declared in this scope: show(5);

Then my question - is this absolutely safe? Can it result in creating a "bad situation"?

xinaiz
  • 7,744
  • 6
  • 34
  • 78
  • All the text in your post is wrong .. the question would be improved by deleting that and just asking your question – M.M Nov 25 '15 at 23:01
  • What if the array is called x? – user253751 Nov 25 '15 at 23:01
  • You need `#include ` to fix the error about `begin` – M.M Nov 25 '15 at 23:02
  • Macros are rarely _safe_, they're just providing simple text processing. Check the results compiling your code just using the cpp preprocessor. – πάντα ῥεῖ Nov 25 '15 at 23:02
  • Macros like this should at least use the [do...while(0) trick](http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros) or similar – M.M Nov 25 '15 at 23:03
  • `show(1,2)` won't work either (the comma is grammatically an argument separator there , not part of an argument) so this macro is awkward to use even if implemented better – M.M Nov 25 '15 at 23:05
  • @M.M I'm not seeing the problem there. The same thing would happen with a function. – user253751 Nov 25 '15 at 23:20
  • @immibis You could make the function accept variadic template – M.M Nov 25 '15 at 23:24

1 Answers1

10

You can have a 100% C++ template function instead, in which the array does not decay to a pointer, by passing the array by reference and having its size and type deduced:

template<typename T, size_t N>
void print_arr(const T(&arr)[N])
{
    for(auto&& elem: arr)
        std::cout << elem;
}

Live on Coliru

A macro is rarely safe, so prefer using (if can) a proper function, which is type checked etc.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    This is useful info but doesn't answer the question – M.M Nov 25 '15 at 23:04
  • @M.M Agree with you, and usually I don't respond with these kind of answers, yet now I am still tempted to leave it here since many people have no idea you can actually pass the array by reference. The absolutely correct answer to the question per se is NO, macros are not safe, some reasons being mentioned in the comments. – vsoftco Nov 25 '15 at 23:05
  • Yes, this does answer the question. In the last sentence. It goes further and corrects the incorrect premise of the question, which makes it strictly superior to an answer which just answers the question. – Benjamin Lindley Nov 25 '15 at 23:12
  • @vsoftco I'm not that much into macro functions, but I just couldn't resist to ask :) I didn't know the one you presented above, nor about passing array by reference. Thanks for your answer. – xinaiz Nov 25 '15 at 23:13