I have tried to compile this (--std=c++0x) using:
[FAIL with --std=c++0x flag] clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
[FAIL without --std=c++0x flag] clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
[FAIL without --std=c++11 flag] Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM
3.3svn)
[FAIL with --std=c++11 flag] Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM
3.3svn)
[PASS with --std=c++0x flag] gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
[FAIL without --std=c++0x flag] gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
When it fails at clang ubuntu, the following errors are produced:
test.cpp:26:37: error: no viable conversion from 'const P' to 'timeval'
return static_cast<timeval>(_p);
^~
/usr/include/x86_64-linux-gnu/bits/time.h:30:8: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from 'const P' to
'const timeval &' for 1st argument
struct timeval
^
/usr/include/x86_64-linux-gnu/bits/time.h:30:8: note: candidate constructor
(the implicit move constructor) not viable: no known conversion from 'const P' to 'timeval &&' for
1st argument
struct timeval
^
test.cpp:9:5: note: candidate function
operator const timeval() const {
^
test.cpp:13:5: note: candidate function
operator const int8_t() const {
^
I am not sure what I am doing incorrectly.
#include <iostream>
#include <cstdint>
union P {
timeval _timeval;
int8_t _int8_t;
uint8_t _uint8_t;
operator const timeval() const {
return _timeval;
}
operator const int8_t() const {
return _int8_t;
}
};
struct Y {
operator const int8_t() const {
return static_cast<int8_t>(_p);
}
operator const timeval() const {
return static_cast<timeval>(_p);
}
P _p;
};
int main()
{
Y testobj;
timeval ret = static_cast<timeval>(testobj);
return 0;
}
As a starting point, you're missing the # from the beginnings of your #includes.
#include <iostream>
#include <cstdint>
You're also not including any header that should define the type timeval. Given the system(s) you're apparently using, you probably want:
#include <sys/time.h>
If you were using Windows, that would probably be:
#include <winsock2.h>
There are probably more problems, but that should at least get you started in the right general direction.
You may need to remove the first const from the conversion operators, because you are trying to cast the union to a non-const value later:
operator timeval() const {
return _timeval;
}
Abusing conversion operators seems like a bad idea. If you want encapsulation, consider creating proper methods like timeval getTimeval() const.
Related
This code happily compiles with:
Apple clang version 13.0.0 (clang-1300.0.29.3)
#include <string>
#define FMT_HEADER_ONLY
#include <fmt/core.h>
using namespace std;
int main()
{
string abc = "{}\n"s;
printf("%s", fmt::format(abc, 12).c_str());
return 0;
}
It fails with:
Ubuntu clang version 13.0.0-2
fmttest.cpp:10:18: error: constexpr variable cannot have non-literal
type 'const std::string' (aka 'const basic_string<char, char_traits<char>, allocator<char>>')
constexpr string abc = "{}\n"s;
Same -stdlib=libc++ -std=c++2b given as arguments.
Why two presumably identical versions of the compiler behave so differently?
What am I missing?
I am trying to start a thread t:
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
void function(int p1, int p2, int p3){
std::cout<<p1<<p2<<p3<<std::endl;
}
int main(int argc, char const *argv[]) {
std::cout<<"starting"<<std::endl;
std::thread t(function, 1, 2, 3);
std::cout<<"created thread"<<std::endl;
t.join();
std::cout<<"end"<<std::endl;
return 0;
}
My compiler tells me this:
doesntwork.cpp:12:15: error: no matching constructor for
initialization of 'std::thread'
std::thread t(function, 1, 2, 3);
^ ~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:408:9: note:
candidate constructor template not viable: requires single
argument '__f', but 4 arguments were provided
thread::thread(_Fp __f)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:289:5: note:
candidate constructor not viable: requires 1 argument, but 4
were provided
thread(const thread&);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:296:5: note:
candidate constructor not viable: requires 0 arguments, but
4 were provided
thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
^
1 error generated.
In the first case it tells me that for the thread t, there is no constructor that can use more than 1 parameter, while if I just remove the arguments (p1, p2, p3) it doesn't work either because I am not passing any argmuent....
Compiler information:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
built command used: g++ doesntwork.cpp -o doesntwork.out
Is there something different you have to do when compiling with threads? Am I missing something very obvious?
On macOS, g++ (from Xcode: Version 10.0 (10A255)) is aliased to clang which by default does not work with c++11 threads. To solve the problem you have to use the -std=c++11 switch.
Example:
g++ -std=c++11 fileToCompile.cpp -o outputFile.out
This should let you compile c++ code using c++11 threads.
Thank you to #M.M for providing the answer above in the comments.
I have a MVE program that compiles and runs with g++-5.2.0 but not with clang-602.0.53. The program tries to assign a lambda expression to a type alias of compatible type.
#include<iostream>
#include<complex>
using std::cout;
using std::endl;
using std::complex;
// type alias
using CfDD = complex<double> (*) (double);
// lambda of compatible type
auto theLambda = [] (double _) {return complex<double>({1,0});};
int main()
{ // Show that the lambda is callable:
cout << theLambda(3.14) << endl;
// Show that the lambda is assignable to a var of type CfDD
CfDD cfdd = theLambda;
cout << cfdd (3.14) << endl;
}
This program works when compiled with g++-5.2.0:
$ g++-5 --version
g++-5 (Homebrew gcc 5.2.0) 5.2.0
...
$ g++-5 -std=c++14 main.cpp && ./a.out
(1,0)
(1,0)
But produces an error that I don't understand and don't know how to fix under clang:
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with- gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
...
$ gcc -std=c++14 main.cpp
main.cpp:10:40: error: ambiguous conversion for functional-style cast from 'void' to
'complex<double>'
auto theLambda = [] (double _) {return complex<double>({1,0});};
^~~~~~~~~~~~~~~~~~~~~
... candidate is the implicit move constructor
class _LIBCPP_TYPE_VIS_ONLY complex<double>
^
... candidate is the implicit copy constructor
candidate constructor
complex<double>::complex(const complex<float>& __c)
What does this error mean and why doesn't this code compile?
When you wrote:
return complex<double>({1,0});
we look at the valid constructors for std::complex<double> and find:
constexpr complex(double re = 0.0, double im = 0.0); // (1)
constexpr complex( const complex& other ); // (2)
constexpr complex(const complex<float>& other); // (3a)
explicit constexpr complex(const complex<long double>& other); // (3b)
We're constructing this object with an initializer list. So (1) isn't viable, and neither is (3b) since that constructor is marked explicit. The other two, however, are both viable since both complex<double> and complex<float> can be constructed from two ints. Neither is better than the other, which is why clang complains about "ambiguous conversion".
The easiest solution is to drop the unnecessary {}s:
return complex<double>(1,0);
Note that you don't need to name the argument _, you can just not provide a name for it.
NOTE: this question was caused by a bug in clang
In attemping to write a function taking an arbitrary number of intitializer_lists whose types need not match, I've stumbled upon a strange error:
template <typename... Ts>
void function(std::initializer_list<Ts> && ...args){
}
int main() {
function({1,2,3}, {'h', 'w'}, {"hello", "aloha"});
return 0;
}
The string literals cause a problem under clang but not under gcc with -pedantic -Wall -Wextra not warning about any extensions.
clang produces the error:
error: no matching function for call to 'function'
function({1,2,3}, {'h', 'w'}, {"hello", "aloha"});
^~~~~~~~
note: candidate function [with Ts = <int, char, char const[6]>] not
viable: no known conversion from 'const char [6]' to 'char const[6]' for
3rd argument
void function(std::initializer_list<Ts> && ...args){
^
So a few questions:
Is clang wrong for rejecting this or is gcc using an extension to deduce an initializer_list of arrays? Or is it deeper where gcc is just passing bad code?
Is this is the equivalent of passing an initializer_list of initializer_lists which is of course not allowed?
What is the difference between const char[6] and char const[6]?
Details:
clang version 3.4 (http://llvm.org/git/clang.git 9a65f4251fe5548e0b4478d584796ca84a6f5ebc) (http://llvm.org/git/llvm.git 4f67afc3d67d9a68f1b37767c9c2966a775186bd)
Target: x86_64-unknown-linux-gnu
Thread model: posix
gcc (Debian 4.7.2-5) 4.7.2
Debian 7
This is a bug in clang: it fails to perform the required array-to-pointer decay when deducing {"hello", "aloha"} against std::initializer_list<Ts>, but only when Ts is a parameter pack.
This program compiles fine:
#include <initializer_list>
template <typename T>
void function(std::initializer_list<T> il) {
}
int main() {
function({"hello", "aloha", "foobarbaz"});
}
but this program triggers the bug:
#include <initializer_list>
template <typename... T>
void function(std::initializer_list<T>... il) {
}
int main() {
function({"hello", "aloha", "foobarbaz"});
}
Edit: Could not find this bug in the LLVM bugzilla tracker, submitted as bug# 18047.
I try to compile the simple code
#include <atomic>
int bar = 0;
void foo(std::atomic<int>&flag)
{ bar = flag; }
with clang++ 3.2 (downloaded as llvm 3.2 from llvm.org; on mac os.x 10.8.3 this fails with the error
/> clang++ -std=c++11 -stdlib=libc++ -O3 -march=native -c test.cc
In file included from test.cc:1:
/usr/include/c++/v1/atomic:576:17: error: first argument to atomic operation must be a pointer to non-const _Atomic type ('const _Atomic(int) *' invalid)
{return __c11_atomic_load(&__a_, __m);}
^ ~~~~~
/usr/include/c++/v1/atomic:580:53: note: in instantiation of member function
'std::_1::_atomic_base::load' requested here
operator _Tp() const _NOEXCEPT {return load();}
^
test.cc:5:9: note: in instantiation of member function 'std::_1::_atomic_base::operator int' requested here
bar = done;
When I use /usr/bin/clang++ instead (which comes with the OS or Xcode) it compiles just fine. The libc++ is that at /usr/lib/c++/v1 in both cases.
What am I missing? Is there another libc++ that comes with llvm 3.2 but which I'm missing? (I cannot find anything in the clang3.2 tree).
Xcode now bundles libc++ within the Xcode.app directory. You can inspect this directory by control-clicking Xcode.app and choose "Show Package Contents".