0

Using g++ 11.2.0 for x86_64, I came across a syntax error in my code while showing it to a colleague, and we were both rather confused that this did not cause an error during compilation time. The binary runs as expected with no further errors.

    char uuidString[37] = {0};
    Extensions::uuid_t uuidRaw = {0};
    Extensions::Uuid::generateRandomUuid().getUuid(&uuidRaw);
    Extensions::uuid_unparse(uuidRaw, uuidString), // <- note the comma

    cout << "Chunk context ID: " << uuidString << endl;

Extensions::Uuid is just a simple wrapper class around libuuid, and libuuid is #included in the Extensions namespace.

I've recompiled the application and the compiler suite generates this (end) output:

[ 99%] Building CXX object CMakeFiles/cc_data_chunking_ref_impl.bin.dir/src/TestImage.cpp.o
[100%] Building CXX object CMakeFiles/cc_data_chunking_ref_impl.bin.dir/src/Main.cpp.o
[100%] Linking CXX executable cc_data_chunking_ref_impl.bin
[100%] Built target cc_data_chunking_ref_impl.bin
(x86_64) (feat/test-cases-data-chunking) simon@simon-kubuntu-dev:~/app/test/data_chunk_ref_impl/build$ date
Di 26. Apr 10:08:27 CEST 2022

The application generates output as expected and the algorithms also work as expected.

Total file size: 497792B
Packing test data into DataContainer...
DataContainer has size 497853B
Chunk context ID: b16a03ac-8687-4608-a04c-04e1da81040b
Processing chunk 1/5
Meta data: 
mimeType: "image/jpeg"
machineSerialNo: 12345678
deviceSerialNo: "1234567890"
md5OfPayload: "7092d252de0649f54f55d6f69a9b1328  -"

Why doesn't the comma cause compilation to terminate, even though replacing a different semicolon with a comma causes the expected behaviour?

    const int32_t totalChunks = std::ceil(data.size() / chunkSize), // <-- comma now here

    char uuidString[37] = {0};
    Extensions::uuid_t uuidRaw = {0};
    Extensions::Uuid::generateRandomUuid().getUuid(&uuidRaw);
    Extensions::uuid_unparse(uuidRaw, uuidString),

    cout << "Chunk context ID: " << uuidString << endl;

Compiler output:

[100%] Building CXX object CMakeFiles/cc_data_chunking_ref_impl.bin.dir/src/Main.cpp.o
/app/test/data_chunk_ref_impl/src/Main.cpp: In function ‘int32_t writeChunkedFile(const string&, int32_t)’:
app/test/data_chunk_ref_impl/src/Main.cpp:125:5: error: expected unqualified-id before ‘char’
  125 |     char uuidString[37] = {0};
SimonC
  • 1,547
  • 1
  • 19
  • 43
  • 1
    [The comma operator](https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator) can be used to sequence expressions. And both `Extensions::uuid_unparse(uuidRaw, uuidString)` and `cout << "Chunk context ID: " << uuidString << endl` are expressions, and can thus be sequenced with the comma operator. – Some programmer dude Apr 26 '22 at 08:16
  • But that doesn't really explain why replacing a different semicolon with a comma causes the expected behaviour. Or I just didn't understand the comma operator yet. – SimonC Apr 26 '22 at 08:18
  • Especially considerung uuid_unparse is a void function, so doesn't return any value to be evaluated – SimonC Apr 26 '22 at 08:19
  • Because in the second case you try to use two definition statements separated by comma, which is not valid. It's the same as `int a = 5, float b = 10.0;` which clearly isn't valid. – Some programmer dude Apr 26 '22 at 08:20
  • Also, in `expression1, expression2` the result of `expression1` (if any) is discarded (not used). Please read the linked duplicate question as well as the linked reference. – Some programmer dude Apr 26 '22 at 08:23
  • C++ is freeform. Indentation and formatting doesn't affect parsing. It is no longer two statements after the replacement with a comma, but one. – StoryTeller - Unslander Monica Apr 26 '22 at 10:17

0 Answers0