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.
Related
I'm just trying to figure out how I can use clang++ to compile this:
for (int i : std::ranges::iota_view{1, 10})
std::cout << i << ' ';
The latest Apple Clang failed to recognize iota_view, and so I downloaded LLVM 15.0.7 using Homebrew. This suggests that 15.0.7 is compatible with ranges, but I'm still having trouble with it not being recognized, even with the -fexperimental-library, -std=c++20, and -stdlib=libc++ flags. Anyone know how to resolve this, short of just using gcc instead?
It works with LLVM experimental C++ standard library.
#include <ranges>
#include <iostream>
#include <algorithm>
int main() {
for (int i : std::ranges::iota_view(1, 10))
std::cout << i << ' ';
std::cout << '\n';
}
clang main.cc -std=c++20 -stdlib=libc++ -fexperimental-library
./a.out
1 2 3 4 5 6 7 8 9
https://godbolt.org/z/5frq61K4Y
See also libc++ C++20 Status.
I am compiling the code on solaris 5.11.
G++ version is 4.8.2.
The same code works on Ubuntu but gives the error: 'to_string() was not declared in this scope' on solaris.
I went through many links and tried the following things:
Adding 'std::' before to_string(). This gives error - 'to_string is not a member of std'
Added 'std=c++11' or 'std=c++0x' while compilation.
Both the above things do not work.
Is there anything related to Solaris?
The actual code was very huge. So simulating the error in sample code below.
temp.cpp
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int i = 10;
str = "john age is " + to_string(i);
cout << str;
return 0;
}
command: g++ temp.cpp -std=c++0x -o temp
For GCC 4.8.2 the to_string functions are defined conditionally, according to the following:
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
The GLIBCXX_USE_C99 macro depends on a large number of C99 functions being supported by the OS, so presumably the necessary C99 library functions were not found when building GCC on Solaris. So the to_string definitions are absent.
In current versions of GCC the condition is more fine-grained, and checks whether the C99 functions are defined in C++98 mode and C++11, so that the absence of any C99 function doesn't disable everything:
#if __cplusplus >= 201103L
//...
#if _GLIBCXX_USE_C99_STDIO
It's not possible to backport these improvements to GCC 4.8, so you might need to update to at least GCC 6.
compile using std=c++11 as below
g++ -std=c++11 filename.cc
Note : your compiler must support c++11
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 have a C++ program that will not compile under OS X 10.8.5 with the g++ compiler. The problem seems to be with the math.h header file.
This is the version of g++ is
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/Users/densmore3/local/usr/local/bin/../libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable- languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
There are 40-50 errors of the type below. The code compiled fine on 10.6. What is going on?
/Users/xxxx/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:203:1: error: ‘__header_always_inline’ does not name a type
__header_always_inline int __inline_isfinitef(float);
Users/densmore3/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:580:27: error: expected initializer before ‘__AVAILABILITY_INTERNAL__MAC_10_0_DEP__MAC_10_9’
extern float __inff(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
Here is a piece of test code that gives the same error as my real code. The error goes away if I remove the math.h include statement.
#include <iostream>
#include <math.h>
#include <stdio.h>
//#include <complex>
//#include <vector>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
The compile command I am using is:
g++ test.cpp
gcc-4.8 is the correct version to use on OS X 10.8.
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.