Why gcc doesn't recognize initialization like "unsigned int()" for C++11? - c++11

int main()
{
auto k = int();//ok
auto i = unsigned int();//gcc error
return 0;
}
The line of "auto i" leads to gcc reporint an eror:
error: expected primary-expression before 'unsigned'
While VC doesn't have problem on my code. Why is this, is this a bug of gcc or, should it use some extra parences?
Thanks.

Related

Failed to compile OpenVDB on Windows(VS2017)

I tried to compile OpenVDB on Windows11(VS2017), but confusing error occurs:
Error C2676 Binary '<': 'const _Ty' does not define the operator or a conversion to a type that can be received by a predefined operator openvdb_shared
Error C2056 invalid expressions
Error C2088 invalid for class
It points to <algorithm.h> file, line 5386: "_NOEXCEPT_COND(_NOEXCEPT_OPER(_Left < _Right))".
#pragma warning(push)
#pragma warning(disable: 28285) // (syntax error in SAL annotation, occurs when _Ty is not an integral type)
template<class _Ty>
_Post_equal_to_(_Left < _Right ? _Right : _Left)
_NODISCARD constexpr const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)
_NOEXCEPT_COND(_NOEXCEPT_OPER(_Left < _Right))
{ // return larger of _Left and _Right
if (_Left < _Right)
{
_STL_ASSERT(!(_Right < _Left), "invalid comparator");
return (_Right);
}
return (_Left);
}
and anothor fatal error:
fatal error LNK1104: can not open file “....\openvdb\openvdb\Debug\openvdb.lib”
I download OpenVDB source code and dependencies by git and vcpkg following the manual OpenVDB. I have no idea about how to fix those problems. Any suggestions or solution would be greatly appreciated!!!

Why doesn't alignas compile when used in a static declaration with clang?

I have a compilation error on clang and a warning with gcc with this code:
static alignas(16) int one_s = 1; // clang: error: an attribute list cannot appear here; gcc: warning: attribute ignored;
static __attribute__((aligned(16))) int zero_s = 0; // on the other hand this works well on both compilers...
alignas(16) int one = 1; // this also works on both compilers
__attribute__((aligned(16))) int zero = 0; // as well as this
Does anyone know why alignas is not accepted in the declaration that contains the static keyword? I used the --std=c++11 compiler option with both gcc and clang. (Edit: I used clang 3.4 and above and gcc 4.8 and above)
Note that when compiling with Visual Studio (CL 19 RC) I don't get an error when using alignas in a static declaration like that.

MSVC fails on static_assert on struct member

Here's my test case, simplified to a minimal test case:
#include <iostream>
struct Foo {
int i;
static_assert(sizeof(i) > 1, "Wrong size");
};
static_assert(sizeof(Foo::i) > 1, "Wrong size");
int main () {
Foo f{42};
static_assert(sizeof(f.i) > 1, "Wrong size");
std::cout << f.i << "\n";
}
This works fine on any version of GCC or Clang recent enough to support static_assert. But on MSVC 2015, the first static_assert gives me a compile error:
static-assert.cpp(4): error C2327: 'Foo::i': is not a type name, static, or enumerator
static-assert.cpp(4): error C2065: 'i': undeclared identifier
static-assert.cpp(4): error C2338: Wrong size
The other two asserts work as expected, if I remove the first one. I also tried it on VS2013 with the same results. The documentation for C2327 talks about nested class member access, which doesn't seem relevant in any way I can see. What's going on here? Which compiler is right?
(Edited to add a third assert to make the problem clearer.)
Further edit: It doesn't actually seem to have anything to do with static_assert, because this fails with the same error:
struct Foo {
int i;
char array[sizeof(i)];
};
Again, this works fine in other compilers.
I had a simular issue and the problem was that MSVC 2013 does not support constant expressions thus both
static_assert(sizeof(i) > 1, "Wrong size");
and
char array[sizeof(i)];
require constant expressions which is not supported by the compiler.
This is valid in gcc though
Interestingly, sizeof (decltype(i)) works in VS2013.

How to enable the _Generic keyword

I have this test source:
#include <stdio.h>
int main()
{
int x;
printf("x=%d\n", _Generic('x', int: 1, default: 0));
return 0;
}
Compiling with c++ (from GCC 4.9.2) fails:
t.cpp: In function ‘int main()’:
t.cpp:7:33: error: expected primary-expression before ‘int’
printf("x=%d\n", _Generic('x', int: 1, default: 0));
^
t.cpp:7:41: error: expected primary-expression before ‘default’
printf("x=%d\n", _Generic('x', int: 1, default: 0));
^
t.cpp:7:51: error: ‘_Generic’ was not declared in this scope
printf("x=%d\n", _Generic('x', int: 1, default: 0));
The compiler arguments are:
c++ --std=c++11 t.cpp -o t
What am I doing wrong?
_Generic is a C11 feature. It is not present in C++ (any version at least up to C++14 - I don't really expect it to be added either).
If you want to use it, you'll need to write C code, and use a compiler that supports that standard (reasonably recent versions of gcc and clang do for example, using -std=c11).
If you want to write C++, use overloading or templates instead, for example:
#include <iostream>
int foo(int) { return 1; }
int foo(char) { return 0; }
int main()
{
std::cout << "x=" << foo('x') << std::endl;
}
This prints x=0 in C++, the foo(char) overload is the best match.
Note that there's difference between C and C++ that might trick you here too: 'x' is a char in C++. It's an int in C. So if _Generic had been implemented (maybe as an extension) by your compiler, chances are you'd get different output when compiling your example as C versus compiling as C++.
Here's the C++ form (forgive me for using the using directive, I know its bad form):
#include <iostream>
using namespace std;
template< typename T> T do_something(T argument) {
// Put here what you need
}
int main()
{
int x;
cout << "x" << (x = do_something(x));
return 0;
}
_Generic is C11, you're probably using a C++ compiler when you meant to use a C compiler.

clang pass variables by reference

I am compiling the following code using clang 3.4.2 ..
#include <stdio.h>
void haa(int& j){
j=1;
}
int main(){
printf("hello\n");
}
This gives the following error:
hello.c:3:13: error: expected ')'
void haa(int& j){
^
hello.c:3:9: note: to match this '('
void haa(int& j){
^
hello.c:3:13: error: parameter name omitted
void haa(int& j){
^
hello.c:4:2: error: use of undeclared identifier 'j'
j=1;
^
3 errors generated.
Compiling the same with gcc gives no errors or warnings...
Can someone explain why this is happening?
The issue is that pass by reference (with references and not pointers) is not a c but a c++ feature.
You need to compile the code with a c++ compiler such as g++ or clang++. Changing the file extension to .cpp also works, as this tells the compiler to treat it as a c++.

Resources