Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S-suffix string literal in c++ gives error

Tags:

c++

Why does this utterly simple code give error:

#include <string>  
using namespace std::string_literals;
int main() {
    auto s = "cat"s;
}

Give this error:

hello.cpp:4:16: error: expected ';' at end of declaration
        auto s = "cat"s;

Is this compiler specific because I am reading from official resource like : https://msdn.microsoft.com/en-us/library/69ze775t.aspx.

My machine runs osx I use terminal and g++ to compile:

g++ -v 

spits out:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I compiled it with

g++ -o hello hello.cpp

and

 g++ -std=c++11 hello.cpp -o hello

and they both show errors.

Any ideas?

like image 737
Meta Xenology Avatar asked Mar 10 '23 10:03

Meta Xenology


1 Answers

You need to use -std=c++14.

g++ -std=c++14 hello.cpp -o hello

gcc 7.0.0 with C++11: http://melpon.org/wandbox/permlink/1bqQhMueFpI8xVJq

gcc 7.0.0 with C++14: http://melpon.org/wandbox/permlink/Tf23uFqatmXkSss2

like image 112
Jack Deeth Avatar answered Mar 20 '23 05:03

Jack Deeth