I try to make uncrustify to go from:
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
to :
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
But so far I failed miserably! Is this even possible, and if so, can I get any hints?
No, there is no such angle align option in Uncrustify as of now.
Only
# Whether to align lines that start with '<<' with previous '<<'.
#
# Default: true
align_left_shift = false # true/false
which for some reason is defaulted be always used
Related
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
Tried to do it based on the official documentation. Here is the code, I'm trying to add a few items to an array:
auto in_array = bsoncxx::builder::stream::document{}
<< "$set" << open_document << call_id_key << call_id << close_document << "$push"
<< open_document << transcription_key
<< open_array << open_document << direction_key << "INBOUND" << text_key
<< transcript << confidence_key << confidence
<< start_time_key << start_time << end_time_key << end_time
<< "words" << open_array;
for (const auto& word_info : words) {
in_array = in_array << open_document << "word" << word_info.first <<
"confidence" << word_info.second << close_document; // Assertion happens here
}
bsoncxx::document::value doc = in_array << close_array << close_document << close_array << close_document << finalize;
std::cout << bsoncxx::to_json(doc.view()) << std::endl;
This question already has answers here:
cout not printing unsigned char
(5 answers)
Closed 2 years ago.
I recently noticed numeric_limits::max() and numeric_limits::min() don't seem to work for uint8_t and int8_t. Is there a reason for this or could it be a bug? I tried on my own computer using gcc compiler:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;
std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl;
std::cout << "numeric_limits<uint32_t>::max() = " << numeric_limits<uint32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::max() = " << numeric_limits<int32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::min() = " << numeric_limits<int32_t>::min() << std::endl;
std::cout << "numeric_limits<uint64_t>::max() = " << numeric_limits<uint64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::max() = " << numeric_limits<int64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::min() = " << numeric_limits<int64_t>::min() << std::endl;
return 0;
}
gives output:
numeric_limits<uint8_t>::max() = �
numeric_limits<int8_t>::max() =
numeric_limits<int8_t>::min() = �
numeric_limits<uint16_t>::max() = 65535
numeric_limits<int16_t>::max() = 32767
numeric_limits<int16_t>::min() = -32768
numeric_limits<uint32_t>::max() = 4294967295
numeric_limits<int32_t>::max() = 2147483647
numeric_limits<int32_t>::min() = -2147483648
numeric_limits<uint64_t>::max() = 18446744073709551615
numeric_limits<int64_t>::max() = 9223372036854775807
numeric_limits<int64_t>::min() = -9223372036854775808
It does work. The output is interpreted as ASCII characters though. If you cast to int before you print, you will see the correct values:
std::cout << "numeric_limits<uint8_t>::max() = " << static_cast<int>(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << static_cast<int>(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << static_cast<int>(numeric_limits<int8_t>::min()) << std::endl;
std::cout << "numeric_limits<uint8_t>::max() = " << std::to_string(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << std::to_string(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << std::to_string(numeric_limits<int8_t>::min()) << std::endl;
try to convert them to string, before inserting them into cout.
int8 types are probably defined as chars, so don't print the values as charbut as ints:
int main() {
std::cout << "numeric_limits<uint8_t>::max() = " << (int)numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << (int)numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << (int)numeric_limits<int8_t>::min() << std::endl;
}
I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);
I'm a beginner to C++ and I'm having a problem with this code, which is supposed to display the scores during the Superbowl final:
#include <iostream>
enum POINTS { EXTRA_POINT = 1, SAFETY = 2, FIELD_GOAL = 3, TOUCHDOWN =6 };
unsigned short giantsScore = 0, patriotsScore = 0;
int main()
{
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + SAFETY << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + TOUCHDOWN + EXTRA_POINT << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + TOUCHDOWN + EXTRA_POINT << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + TOUCHDOWN + EXTRA_POINT << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + FIELD_GOAL << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore = giantsScore + FIELD_GOAL << "\n";
std::cout << " Patriots: " << patriotsScore << "\n\n";
std::cout << " Giants: " << giantsScore << "\n";
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT << "\n\n";
return 0;
}
Ignoring that this is quite inelegant, when I run this through the compiler, G++, I get the error message
error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'
If I remove the constants and add them in before each std::cout, then it runs fine. I just wanted to know why I can't add the constants during each output line?
Your error message states: int << char, which of course is an odd operation.
It is because of operator priorities.
Each operator has a priority meaning it will evaluate before or after the other operators are evaluated.
+ evaluates before =
and << should be evaluated after = had cout<<"stuff" been its original purpose.
<< is originally the bit-shift operator (still is), so that is why you are experiencing this odd behaviour. Add parentheses and you'll be good.
Check http://cs.smu.ca/~porter/csc/ref/cpp_operators.html for an overview of the priority rules of operators. When you write this:
std::cout << " Patriots: " << patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT << "\n\n";
Then according to the priority rules, the + operator will be executed first, giving you this:
std::cout << " Patriots: " << patriotsScore = result << "\n\n";
Then the << operator is executed, which means also `result << "\n\n". But this operator is not defined between int and char[2].
To solve your problem, put parenthesis around the assignment operation, like this:
std::cout << " Patriots: " << (patriotsScore = patriotsScore + FIELD_GOAL + EXTRA_POINT) << "\n\n";