I am seeing a weird crash trying to use a wrapper template class around aligned struct/class. The following program is crashing when compiled with clang, while working properly with both gcc and visual studio.
struct __attribute__( ( aligned( 16 ) ) ) AlignedStruct
{
AlignedStruct( ) { }
float x,y,z,w;
};
template <typename P1> class Wrapper
{
public:
P1 Inner;
};
int main( )
{
Wrapper<AlignedStruct> * t1 = new Wrapper<AlignedStruct>( );
return 1;
}
The crash seems to be while calling AlignedStruct constructor (if I remove it, clang compiled code runs as well).
Can anyone explain what is going on here?
Thanks!
Edit:
The crash occurs with clang 3.4.2 under cygwin32 and cygwin64
and clang 3.4.1 under Ubuntu 14.04 (x32) and Ubuntu 14.10 (x32)
It does not, however, occur when testing with clang 3.4.1 under FreeBSD 10.1 (x32) and OS X Yosemite.
I've tried playing around with compilation flags but I can't seem to be able to make any difference with those, so I am just running clang++ file.cpp
Related
Template argument deduction appears to be broken in Clang 6 for temporary objects.
g++ 8.1.0 compiles and runs the example correctly.
Clang 6.0.0 and 6.0.2 both error at the indicated line with this message:
error: expected unqualified-id
Print{1,"foo"s,2}; /********** Broken in Clang **********/
All Other lines work correctly.
The behavior is the same in both cases whether -std=c++17 or -std=c++2a is used.
The Clang c++ Status Page indicates that template argument deduction was implemented as of Clang 5 (P0091R3, P0512R0).
Is this a bug? Are there workarounds (e.g. compiler flags, not code changes)?
example:
template<class ...Ts>
void print(Ts...ts){ (( cout << ... << ts )); }
template<class ...Ts>
struct Print {
Print(Ts...ts){ (( cout << ... << ts )); }
};
int main(){
Print{1,"foo"s,2}; /********** Broken in Clang **********/
Print<int,string,int>{1,"foo"s,2};
auto p1 = Print{1,"foo"s,2};
Print p2{1,"foo"s,2};
print(1,"foo"s,2);
}
This is Clang bug 34091.
Luckily, it is already fixed, and the trunk build of Clang compiles this without issue.
As far as I know, however, there is currently no way to work around this without code changes, short of upgrading to the next Clang release whenever that comes out.
How can I declare a function in a class that uses return type deduction?
This is fine:
auto foo() {
return 5;
}
But this does not work:
class Test {
auto foo();
};
auto Test::foo() {
return 5;
}
internal compiler error: in gen_type_die_with_usage, at dwarf2out.c:19486
I don't know if it has anything to do with it but I am using QtCreator 3.3.
EDIT:
I am using Qt 5.4 and QtCreator 3.3.
I have added CONFIG += c++14 to the project file.
By default, I am using GCC 4.8.2 and I guess this is why I get the error (I need 4.9). However when I use Clang 3.5 (3.4 is needed) it says
error: 'auto' return without trailing return type; deduced return types are a C++1y extension
EDIT2: This seems to be a bug with Qt and not with GCC. Outside of Qt I can write class functions with return type deductions and it compiles and runs just fine with GCC 4.8.2 and Clang 3.5.0
I read it here that CUDA 6.5 has started support for C++11 :
https://groups.google.com/forum/#!topic/thrust-users/R37GIkMG4tk
But when I compile an example code below, I got
$ nvcc -std=c++11 cu-gcc11.cu -o test
nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.
cu-gcc11.cu(7): error: explicit type is missing ("int" assumed)
My setting : CUDA 6.5, g++ 4.5, ubuntu 12.04
Codes :
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
C++11 support in nvcc is experimental at this time. In order to properly use it you will need an appropriate configuration. This is not documented anywhere AFAIK, but you should have good results with either Fedora 20 or Ubuntu 14.04, both of which are supported configs for cuda 6.5 and include GCC 4.8.x.
In your case your GCC version is just too old.
I don't think -std=c++11 was available in GCC 4.5. Try -std=c++0x.
I was struggling with segmentation fault after returning from main in C++0x code compiled under GCC with MinGW, and have narrowed it to simple case:
class A {
public:
virtual ~A();
};
A::~A() {
}
const A a;
int main() {
return 0;
}
The reason is correlated with:
const qualifier before object, removing it stops seg fault,
Lack of constructor in class A - adding empty one stops seg fault.
Version of compialtor - newer TDM MinGW with gcc 4.8.1 does not build code causing seg fault.
Am I triggering some undefined behavior, or really compiler bug is reason of such behavior?
Affected compiler:
gcc --version
gcc.exe (tdm-1) 4.7.1
g++ --version
g++.exe (tdm-1) 4.7.1
ld --version
GNU ld (GNU Binutils) 2.22
This appears to be gcc bug 55893 that was fixed in 4.7.3. The attached testcase is very similar to yours:
struct foo
{
virtual ~foo () { }
};
int main ()
{
static const foo tmp;
}
The error occurs because gcc places the const object in read-only memory, because it is trivially constructible, without checking that the object has a non-trivial destructor. The destructor, being virtual, attempts to update the vtable pointer, leading to the segfault.
I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.