0

When using a lambda expression my compiler shows me an error. This is the implementation (where name is a std::string&):

auto expression = [name](const Item* x) -> bool { return x->get_name() == name; };

This is the error:

items/container.cpp:20:27: error: expected expression
    auto expression = [name](const Item* x) -> bool { return x->get_name() == name; };

These are my compiler flags:

CFLAGS=$(-std=c++11 -stdlib=libc++  -g -Wall -Werror -Wextra -I.)

All other C++11 features I have used have worked so far. I am using MacOS 10.11.3.

>> clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

>> clang++ --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

Unfortunately this did not help me: Clang 3.1 and C++11 support status

UPDATE: After creating a minimal example as suggested by the comments below it worked! The problem must lie in my makefile. This reproduces the error:

CC=clang++
CFLAGS=$(-stdlib=libc++ -std=c++11 -g -Wall -Werror -Wextra -I.)

all: main

main: main.o
    $(CC) $(CFLAGS) -o main *.o

main.o: main.cpp
    $(CC) $(CFLAGS) -c main.cpp

My main.cpp file:

#include <iostream>
#include <string>

int main() {
    auto lambda = [](const std::string& str) { return str == "whatever"; };
    std::string s1 = "something else";
    std::string s2 = "whatever";
    std::cout << "should be false: " << lambda(s1) << std::endl;
    std::cout << "should be true: " << lambda(s2) << std::endl;
    return 0;
}
Community
  • 1
  • 1
vladakolic
  • 280
  • 4
  • 12

1 Answers1

0

You're misusing the $ sign in your makefile. It's for substitutions of variables so $(-stdlib=libc++ ...) would suggest you had a variable whose name started with -stdlib=... (I'm surprised this isn't giving you an error)

If you don't want a substitution and just want literal content, write it after the = sign unescaped:

CC=clang++
CFLAGS=-stdlib=libc++ -std=c++11 -g -Wall -Werror -Wextra -I.

all: main

main: main.o
    $(CC) $(CFLAGS) -o main *.o

main.o: main.cpp
    $(CC) $(CFLAGS) -c main.cpp