Strange C/C++ syntax [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's this C++ syntax that puts a brace-surrounded block where an expression is expected?
I've just come across this strange C/C++ syntax:
#include <stdio.h>
int main() {
printf("%s",
({
static char b__[129];
b__[0] = 55;
b__[1] = 55;
b__[2] = 0;
b__;
})
);
}
This compiles and runs fine using both gcc and g++ (4.5.2). This is the first time I see something like this, and I wonder what exactly this syntax means. I've tried to Google it, but I have no idea what this construct is called.

They're called statement expressions, it's a GNU extension. In your example the result of the expression is b__.

Related

A function call must have a constant value in a constant expression [duplicate]

This question already has an answer here:
Is it a conforming compiler extension to treat non-constexpr standard library functions as constexpr?
(1 answer)
Closed 4 years ago.
I have written a c++ program as blow:
#include <iostream>
int main()
{
constexpr double a = 4.0;
constexpr double b = sqrt(a);
std::cout << b << std::endl;
return 0;
}
When I tried to compile this code with visual studio 2017, I got an error that says a function call must have a constant value in a constant expression. The bad line is "constexpr double b = sqrt(a);".
But when I used g++ to compile the same code, no error was reported.
What's the reason of the error? What's the different between g++ and vc++?
sqrt isn't a constexpr function so can't be used in a constexpr expression. GCC seems to have a special built in version of sqrt which is constexpr. Clang doesn't allow this code either:
https://godbolt.org/z/SvFEAW
sqrt is required to be not a constant expression so constexpr double b = sqrt(a); is not supposed to work. Clang does not build this code as well. You also need to include <cmath> header in order to use this function.
include cmath library since you using a sqrt() function
http://www.cplusplus.com/reference/cmath/

Why do I get this compiler error? [duplicate]

This question already has answers here:
error: unknown type name ‘bool’
(4 answers)
Closed 6 years ago.
I want to understand this error: syntax error before 'bool', on the following code:
typedef struct hdate{
date_arc_u date;
unsigned short time;
bool test;
}PACKED_ST horodate_a
When I change bool to another type there is no error.
I already use bool in others parts of the code without error.
I don't understand this error here ....
It's probably because you are writing C code, and the bool type does not exist in C. Your file extension is probably .c, not .cpp, and your code definitely looks like it was written in C.
Probably your C compiler doesn´t know about bool type.
You can try:
1- Including this at first #include <stdbool.h>
2- Declaring at first typedef enum bool { false, true };

C++ strange namespace lookup behavior [duplicate]

This question already has answers here:
Injected class name compiler discrepancy
(3 answers)
Closed 6 years ago.
namespace fooo {
class Fooo {
public:
int a;
};
}
namespace fooo {
class Test {
public:
Test(Fooo::Fooo *i) {
i->a = 1;
}
};
}
This code compiles fine with clang (any version) but fails with gcc.
Can anyone explain why?
EDIT:
Yes, I know the issue here is kinda obvious but why does clang accept it? The person who told me this said that this is a bug in the standard and that there is a Defect Report. Can anyone point to the actual DR?
The error message from gcc tells you exactly what the problem is:
t.cpp:11:16: error: ‘fooo::Fooo::Fooo’ names the constructor, not the type
Test(const Fooo::Fooo *i) {
^
it is suprising that clang doesn't give an error.

irrklang engine was not declared at void [duplicate]

This question already has answers here:
How to declare a global variable in C++
(5 answers)
Closed 7 years ago.
EDIT 2:
Solved! Use the code below and it worked!
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
Just place the code above at the top of the code. (Maybe the next line of include or namespace)
I'm using irrKlang to play audio but I had a problem:
#include <irrKlang.h>
void playSound() {
engine->play2D("src/Click.wav");
}
int main() {
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
playSound();
engine->drop();
return 0;
}
When I run this code, it show that 'engine' (that in the void) was not declared in this scope.
I test this at int main but it work. The problem is that it only worked at main but not at void.
Anything I can use to fix this error? Or is it a bug?
Thanks in advance.
That is expected. irrklang::ISoundEngine* engine is defined in main function but not in playSound().
A straightforward solution would be to pass engine as an argument
void playSound(irrklang::ISoundEngine* engine) {
engine->play2D("src/Click.wav");
}
and in main call it like this
playSound(engine);

_BLOCK_TYPE_IS_VALID(pHEAD->nBlockUse) error [duplicate]

This question already has an answer here:
What is the behavior of "delete" with stack objects? [duplicate]
(1 answer)
Closed 6 years ago.
I'm new to C++ (I'm most comfortable with node.js and Java, but have done some Python before) and am working through understanding pointers. The following example generates a runtime error when compiled with debug symbols.
#include <iostream>
void main(){
int number = 0;
int * numberPtr = &number;
* numberPtr = 1;
std::cout << number;
delete numberPtr;
}
And the error message:
Debug Assertion Failed!
Program: C:\path\to\executable\main.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Expression: _BLOCK-TYPE_IS_VALID(pHead->nBlockUse)
FWIW, I've gotten the same error when I've compiled with both VS 2008 and 2013.
As Oli Charlesworth said, u did not dynamically allocate numberPtr, therefore you can't delete it. I suggest reading about new and delete and Dynamic Memory Allocation if you intend on coding in C++.