I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install
The compiler looks like it supports them and I can define a new literal. I can call the literal function directly but when I use the literal in my code I get a compiler error.
Auto complete on Xcode even suggest my new literal when typing an underscore after a string :D
Here is the code:
#include <cstring>
#include <string>
#include <iostream>
std::string operator "" _tostr (const char* p, size_t n);
std::string operator"" _tostr (const char* p, size_t n)
{ return std::string(p); }
int main(void)
{
using namespace std;
// Reports DOES has string literals
#if __has_feature(cxx_variadic_templates)
cout << "Have string literals" << endl;
#else
cout << "Doesn't have string literals" << endl;
#endif
// Compiles and works fine
string x = _tostr("string one",std::strlen("string one"));
cout << x << endl;
// Does not compiler
string y = "Hello"_tostr;
cout << y << endl;
return 0;
}
I get the below error:
[GaziMac] ~/development/scram clang++ --stdlib=libstdc++ --std=c++11 test.cpp
test.cpp:22:23: error: expected ';' at end of declaration
string y = "Hello"_tostr;
^
;
1 error generated.
This is the version information for clang
[GaziMac] ~/development/scram clang++ -v
Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.0.0
Thread model: posix
Any help gratefully received :)
I don't have Clang, but Google finds a page listing __has_feature selectors.
Use __has_feature(cxx_user_literals) to determine if support for user-defined literals is enabled.
I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install
That's the problem. Clang 3.1 does not come with XCode 4.5 DP1. Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn) was a cut from Clang trunk between 3.0 and 3.1, before I replaced the broken partial implementation with a working one.
As Potatoswatter observes, the right way to test for this feature in Clang is __has_feature(cxx_user_literals).
Here's what Clang trunk says about your code:
<stdin>:23:16: error: use of undeclared identifier '_tostr'; did you mean 'strstr'?
string x = _tostr("string one",std::strlen("string one"));
^~~~~~
strstr
/usr/include/string.h:340:14: note: 'strstr' declared here
extern char *strstr (__const char *__haystack, __const char *__needle)
^
... which has suggested an inappropriate typo correction, but at least it's a correct diagnostic, and your uses of user-defined literals are accepted.
Related
When I run this code on ideone.com, it prints (2,3):
#include <iostream>
#include <complex>
int main() {
std::complex<double> val = 2 + 3i;
std::cout << val << std::endl;
return 0;
}
But when I use clang on macOS 10.11.6, I get no errors or warnings, but the output is (2,0):
$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
$ clang -lc++ test.cpp && ./a.out
(2,0)
What happened to the imaginary part? Am I doing something wrong?
I believe for this first example the compiler is using a GNU extension:
-fext-numeric-literals (C++ and Objective-C++ only)
Accept imaginary, fixed-point, or machine-defined literal number
suffixes as GNU extensions. When this option is turned off these
suffixes are treated as C++11 user-defined literal numeric suffixes.
This is on by default for all pre-C++11 dialects and all GNU dialects:
-std=c++98, -std=gnu++98, -std=gnu++11, -std=gnu++14. This option is off by default for ISO C++11 onwards (-std=c++11, ...).
When I run it with clang I get (are you using -Wall -pedantic? :)):
warning: imaginary constants are a GNU extension
[-Wgnu-imaginary-constant]
Either way, your code is not standard compliant. To use C++14 literals make the code:
#include <iostream>
#include <complex>
using namespace std::complex_literals;
int main() {
std::complex<double> val = 2.0 + 3i;
std::cout << val << std::endl;
return 0;
}
From the documentation:
These operators are declared in the namespace
std::literals::complex_literals, where both literals and
complex_literals are inline namespaces. Access to these operators can
be gained with using namespace std::literals, using namespace
std::complex_literals, and using namespace
std::literals::complex_literals.
Edit: I'm trying to tell it to work with C++11 by clicking "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
I'm getting stoi was not declared in scope, and I've added c++11 to Code::Blocks; I've added compatibility in Settings -> Compilers -> Compiler flags, but it still keeps giving me that error.
And when I try to do atoi or strtol I get the following error:
C:\Users\user\Desktop\Programming\NewProject\main.cpp|19|error: cannot
convert 'std::string {aka std::basic_string}' to 'const char*'
for argument '1' to 'long int strtol(const char*, char**, int)'|
My code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string numberGuessed;
int numberGuessedint = 0;
do {
cout << "Guess a number between 1 and 10: ";
getline(cin, numberGuessed);
numberGuessedint = stoi(numberGuessed);
cout << numberGuessedint << endl;
} while(numberGuessedint != 4);
cout << "You win!" << endl;
return 0;
}
It is a known bug in MinGW bundled with Code::Blocks.
You can apply a patch: http://tehsausage.com/mingw-to-string
Or download fresh version of MinGW (preferable with threading support, as you lack it too) and replace one you have right now.
To use atoi you need:
numberGuessedint = atoi(numberGuessed.c_str());
I am writing a solution which worked for me. As I found in most of the solutions posted on stack overflow, code blocks earlier versions contain a bug. So I deleted my older code blocks version and installed a new version 17.12 from code blocks website.
Then I just clicked on "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
Settings -> Compilers -> Compiler flags.
It works for me(I am using windows 7).
I'm trying to compile a simple program utilizing literals from the std::literals namespace, but Clang is generating errors when I try to compile it.
The code I'm trying to compile:
#include <string>
#include <iostream>
using namespace std::literals;
int main()
{
std::cout << "Hello World!"s << std::endl;
return 0;
}
and the compilation command:
clang++ -stdlib=libstdc++ -std=c++1y a.cpp
which leads to this output:
a.cpp:4:22: error: expected namespace name
using namespace std::literals;
~~~~~^
a.cpp:8:29: error: no matching literal operator for call to 'operator "" s' with arguments of
types 'const char *' and 'unsigned long', and no matching literal operator template
std::cout << "Hello World!"s << std::endl;
^
2 errors generated.
Using g++ or libc++ are out of the question for various reasons, and I've confirmed that other C++14 features (ie. return type deduction and binary literals) work, so it's not an issue with the compiler, making me believe it involves libstdc++.
What can I do to fix this? I'm on Linux Mint 17.1 if it makes any difference.
Remember to ensure that you're compiling the source according to C++14 (the chrono literals are not provided in C++11).
clang++ -stdlib=libstdc++ -std=c++14 a.cpp
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem
I often use assignment of "longer" typed variables to "shorter" ones, for example int to short or uint32_t to uint8_t. One day i decided to find all such cases in my code using gcc, but found to my amazement that gcc didn't output any warnings!
int long_value;
short short_value;
std::cin >> long_value; // Example input: 32769
short_value = long_value; // MS Visual Studio complains here at warning level 4
std::cout << "Long: " << long_value << '\n'; // My example output: 32769
std::cout << "Short: " << short_value << '\n'; // My example output: -32767
Using gcc -Wall or gcc -Wconversion didn't help (gcc didn't output any warning). Actually, it never output any warning for any input and output type (e.g. long and unsigned char).
I have never found an actual bug in gcc so i am almost sure this behavior has a reason.
So why no warning?
Update: i use gcc 4.1.2.
This feature was added in gcc 4.3 version. Previously this was not available.
I hope you are using gcc version 4.2 or below.
http://gcc.gnu.org/wiki/NewWconversion confirms this.
This bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2707 also talks about it.
I can't reproduce that. Compiling this code with gcc 4.4.5 with -Wconversion, I get
a.cc: In function ‘void f()’:
a.cc:7: warning: conversion to ‘short int’ from ‘int’ may alter its value