1
vector<string> foo(vector<string> s) { return s; }
assert(foo(vector<string>{"hello", "world"}) ==
         vector<string>{"hello", "world"});
  • error: macro "assert" passed 2 arguments, but takes just 1
  • error: ‘assert’ was not declared in this scope

maybe define assert in gcc 11.1.0

#  define assert(expr)                          \
     (static_cast <bool> (expr)                     \
      ? void (0)                            \
      : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))

compiler flag is

 -Wall -std=c++20

1 Answer 1

6

The preprocessor only has a primitive understanding of C++'s syntax, and in particular it sees any commas not enclosed in parentheses as argument separators. There are two commas in your assert call, and only one is enclosed in parentheses, so the macro thinks it's getting two arguments as follows

foo(vector<string>{"hello", "world"}) == vector<string>{"hello"
"world"});

Wrap the expression in parentheses to prevent this.

// Note: Double parens
assert((foo(vector<string>{"hello", "world"}) ==
        vector<string>{"hello", "world"}));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.