|
Maximilien wrote: It seems weird that the compiler accept to not fully initialize the struct.
They (old retired programmers) seem to expect the values to be zero-initialized.
According to cppreference.com:[^] Quote: All members that are not initialized explicitly are empty-initialized.
The old programmers are correct.
|
|
|
|
|
Thanks.
let's hope 0 or empty strings are not valid values!!
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Well, 0 is the best bet.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Maximilien wrote: I'm looking at old code.
You would need to make that more specific.
And provide more context.
Since C didn't initialize them and C++ was initially based on that if it was, for example, code from the 80s then perhaps. But I don't think back then they had array structure initializers.
Maximilien wrote: seem to expect the values to be zero-initialized.
From "The C++ Programing Language Special Edition" book by Stroustrup with a 2nd Printing of March 2000. I believe that book was based on the first ANSI C++ standard.
Section "4.9.5 Initialization"
"If no initializer is specified ...is initialized to 0 of appropriate type"
"Members of arrays and structures are default initialized or not depending on whether the array or structure is static"
Myself looking at that code, with no other context provided, yes it should be initialized to zero ('text' and 'c')
|
|
|
|
|
I typically use {0} to initialize a struct with a number of fields.
It works. I imagine it can be extended to do the above as you provided, but probably because it fills in remainders with zeroes.
I don't know how unsafe it is. I can tell you it works with GCC, Clang, and MSVC (at least as of 2022) in my experience.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
For C code, using {0} is safe, as far as I know, and initializes all bytes of the struct to zero. For C++, I don't know. It looks like maybe all bytes are initialized to zero, as per C, but then constructors are called with an empty argument list. If the first struct member has a constructor, then the compiler will look for a constructor that takes a single int. If no such constructor exists, compilation fails.
Both g++ and clang++ warn about struct members with missing initial values when -Wextra is used.
For C, though, it can be useful, since you can also use named initializers e.g.
struct S {
int i;
double d;
char str[10];
};
struct S s { .d = 123.4; }; will assign 123.4 to s.d, and all other members will be set to zero. You can use named initializers in C++ too, but you still get warnings about struct members with missing initializers.
Keep Calm and Carry On
|
|
|
|
|
Wonder why I don't get any warnings doing that under zephyr, which is pretty darn strict in terms of how it configures GCC. I usually use it to test warnings. I think it's set to -Wall
It's possible I'm not using the {0} technique in any of my major libs, but I could swear I am.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
You do need to use -Wextra to get those warnings:
[k5054@localhost]$ cat example.cpp
struct S
{
int i;
float f;
double d;
};
int main()
{
S s{0};
std::cout << s.f << '\n';
}
[k5054@localhost]$ g++ example.cpp
[k5054@localhost]$ g++ example.cpp -Wall
[k5054@localhost]$ g++ example.cpp -Wextra
example.cpp: In function ‘int main()’:
example.cpp:12:11: warning: missing initializer for member ‘S::f’ [-Wmissing-field-initializers]
12 | S s{0};
| ^
example.cpp:12:11: warning: missing initializer for member ‘S::d’ [-Wmissing-field-initializers]
[k5054@localhost]$
Keep Calm and Carry On
|
|
|
|
|
Thank you. I think I'll add that to my build environment.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
Take a look at the GCC (or clang) documentation for what -Wextra adds: Warning Options (Using the GNU Compiler Collection (GCC)) . In most cases, you probably do want to be aware of all the additional warnings that -Wextra brings. But maybe in the embedded world you're going to be doing something that's going to trigger one of the warnings excessively. In which case you might just want -Wmissing-field-initializers . Or perhaps you might want e.g. -Wextra -Wno-unintialized , which gives you all of -Wextra's goodness, but silences the -Wuninitailzed warnings.
If you've got a section of code that you know is going to generate warnings, and you just want to shut the compiler up you can always investigate the GCC diagnostic pragmas : Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))
Keep Calm and Carry On
|
|
|
|
|
I love C++ because I can do things like this:
template<size_t BitDepth>
using bgrx_pixel = gfx::pixel<
gfx::channel_traits<gfx::channel_name::B,(BitDepth/4)>,
gfx::channel_traits<gfx::channel_name::G,((BitDepth/4)+(BitDepth%4))>,
gfx::channel_traits<gfx::channel_name::R,(BitDepth/4)>,
gfx::channel_traits<gfx::channel_name::nop,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
using screen_t = uix::screen<gfx::bgrx_pixel<32>>;
Now, I've made pixels definable in terms of their memory footprint and what channels they expose, like R, G and B, or C, Y, and Mk or whatever. Above I'm defining a BGRx8888 pixel which DirectX uses for its surfaces. That's 3 8-bit channels with a fourth unused, blue first.
The above code is facilitated by this: gfx/include/gfx_pixel.hpp at master · codewitch-honey-crisis/gfx · GitHub[^]
That defines things like channel traits, and does all the necessary compile time computations to make it work.
The problem is this: It's write-only code, and there's no easy way to remedy it because meta-programming in C++17 and prior is very hackish. C++20 and beyond improve the situation a little, but the heavy reliance on The STL rather than language improvements to facilitate this leaves me wanting on little embedded platforms where the STL might not be complete or even available.
It's cool that with C++ you can even make facilities to enable code like the above, and it's the only language I know of that allows for it, but I really wish there was a cleaner way to do this stuff.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: there was a cleaner way to do this stuff
I give up...why is this specific bit of code better as a template versus a method?
To me a method would be cleaner. And it would allow you to do what you want.
|
|
|
|
|
A method would precompute the arbitrary bitshifts needed to craft a bitmap with the appropriate binary footprint, set individual arbitrary color channels, etc, at compile time? AFAIK a method cannot define a type.
constexpr won't do that. It must be some C++20 or beyond feature I'm not aware of, but to solve this problem in C++17 requires a template.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: It must be some C++20 or beyond feature I'm not aware of
Not at all.
So you want to use certain fixed values in your code, but you don't want to compute them (presumably manually.)
So write an executable that outputs those values as code statements. I suspect
1. That executable (the code) is more obvious in its purpose
2. It can be modified to do the part that you claim is missing.
3. It will make the target code less confusing.
You can even make the execution of that executable part of your build process.
|
|
|
|
|
I'm not using a code generator. C++ already has one built in. It's called template and that's literally what it's designed to do.
Anyway, I'd rather deal with it's weirdness than write code generators for everything, at which point I'd just use C because template and constexpr are the only reason I care about C++ over C.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: I'm not using a code generator. C++ already has one built in. It's called template and that's literally what it's designed to do.
Rather certain that is not why they were specifically added to the language.
The fact that you can do that is ok. Just not what they exist for.
Following agrees with my understanding of the addition.
Template (C++) - Wikipedia[^]
"Major inspirations for C++ templates were the parameterized module"
honey the codewitch wrote: I'd rather deal with it's weirdness than write code generators for everything
One case doesn't mean you would need to to it for all.
And your lament is that it does not have all the functionality you would like for this specific situation. So you can hack around it or find another solution.
honey the codewitch wrote: Anyway, I'd rather deal
Again this is another difference in domain spaces. Maintenance for you is based on what you do because that is the business model that you have created.
For me I must consider how others will maintain what I have created. So for complex solutions I must consider the totality of what some future developer might be dealing with when they look at my code. So I would consider a one off module as being more understandable rather than perhaps several different code paths to work around to a potential solution.
|
|
|
|
|
I do use code generation where template won't suffice, like generating state machines, but it complicates the build process.
In the end metaprogramming evolved from simple GP without intrinsic language support for it and that is ultimately what I'm complaining about.
Newer iterations of C++ post say 20 are starting to rectify this, and make the concepts more "first class"
That will improve readability without complicating the build.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
Forgive the multiple replies but I think maybe I'm not explaining the above.
That is user code in the OP. That defines a pixel type that DirectX can consume.
There are others. I've pasted a swath of additional definitions for other types of pixels that are commonly used in my library.
You can then take a pixel type and do like this
using fb_t = bitmap<bgrx_pixel<32>>;<br />
uint8_t fb_buffer[fb_t::sizeof_buffer(screen_size)];<br />
fb_t fb(screen_size,fb_buffer);
Then when you proceed to draw to fb it's begin() method (pointing to fb_buffer in this case) contains bitmap data of that format based on the definition in the OP.
If it was an 18 bit pixel, as some weird graphics devices support, it would do that too. They aren't necessarily even on clean byte boundaries.
Forgive the following, but hopefully it provides useful context
template<size_t BitDepth>
using rgb_pixel = pixel<
channel_traits<channel_name::R,(BitDepth/3)>,
channel_traits<channel_name::G,((BitDepth/3)+(BitDepth%3))>,
channel_traits<channel_name::B,(BitDepth/3)>
>;
template<size_t BitDepth>
using rgba_pixel = pixel<
channel_traits<channel_name::R,(BitDepth/4)>,
channel_traits<channel_name::G,((BitDepth/4)+(BitDepth%4))>,
channel_traits<channel_name::B,(BitDepth/4)>,
channel_traits<channel_name::A,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
template<size_t BitDepth>
using gsc_pixel = pixel<
channel_traits<channel_name::L,BitDepth>
>;
template<size_t BitDepth>
using yuv_pixel = pixel<
channel_traits<channel_name::Y,((BitDepth/3)+(BitDepth%3))>,
channel_traits<channel_name::U,(BitDepth/3)>,
channel_traits<channel_name::V,(BitDepth/3)>
>;
template<size_t BitDepth>
using yuva_pixel = pixel<
channel_traits<channel_name::Y,((BitDepth/4)+(BitDepth%4))>,
channel_traits<channel_name::U,(BitDepth/4)>,
channel_traits<channel_name::V,(BitDepth/4)>,
channel_traits<channel_name::A,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
template<size_t BitDepth>
using ycbcr_pixel = pixel<
channel_traits<channel_name::Y,((BitDepth/3)+(BitDepth%3))>,
channel_traits<channel_name::Cb,(BitDepth/3)>,
channel_traits<channel_name::Cr,(BitDepth/3)>
>;
template<size_t BitDepth>
using ycbcra_pixel = pixel<
channel_traits<channel_name::Y,((BitDepth/4)+(BitDepth%4))>,
channel_traits<channel_name::Cb,(BitDepth/4)>,
channel_traits<channel_name::Cr,(BitDepth/4)>,
channel_traits<channel_name::A,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
template<size_t BitDepth>
using indexed_pixel=pixel<channel_traits<channel_name::index,BitDepth>>;
template<size_t BitDepth>
using hsv_pixel = pixel<
channel_traits<channel_name::H,(BitDepth/3)>,
channel_traits<channel_name::S,(BitDepth/3)>,
channel_traits<channel_name::V,((BitDepth/3)+(BitDepth%3))>
>;
template<size_t BitDepth>
using hsva_pixel = pixel<
channel_traits<channel_name::H,(BitDepth/4)>,
channel_traits<channel_name::S,(BitDepth/4)>,
channel_traits<channel_name::V,((BitDepth/4)+(BitDepth%4))>,
channel_traits<channel_name::A,(BitDepth/4),0,(1<<(BitDepth/4))-1,(1<<(BitDepth/4))-1>
>;
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
EDIT
OK using g - as in "global" shoud work , but it does not
result = BTUL->EditLine_RegExp(result, "(\\w+ \g)");
Where is my error ??
I am having mental block - forgot how to add " match all words ":.
Here is my failing attempt to do so
result = BTUL->EditLine_RegExp(result, "(\\w+*)" );
Can somebody help me to modify my regular expression to
match all words
PLEASE
No references to AI regular expression generators
- they do not do well multiple entries.
modified 26-Oct-23 13:23pm.
|
|
|
|
|
Are you sure you want \w That includes 0-9 and underscore (_ ), as well as alphabetic characters, which may not be what you want.
Is your intention to capture a set of all words in the input text? Is that what BTUL->EditLine_RegExp() does? I googled for EditLine_RegExp , and got precisely zero results, so maybe you can explain where that comes from and what it returns?
Keep Calm and Carry On
|
|
|
|
|
Try this:
Match all words[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The following will find all words:
/\w+/g
You can test it quite quickly at RegExr: Learn, Build, & Test RegEx[^].
But it would help if you showed us the actual text you are working with, and the results you get.
And please use <pre> tags around the code parts so your question is clear.
|
|
|
|
|
Many thanks for all the support.
FYI
I did use "[ -~]+" to extract first continuous words. Now I am working on to extract ALL words...
I did try pass "[/\w+/g]+" to my otherwise working function and ended up with [/w+/g]+
- the back slash is missing,
And this is result , part of my debug messages
" instring text \t\n Waiting to connect to bluetoothd..."
" regular expression \t\n [/w+/g]+"
" Has all match g"
You asked for the string I am trying to extract stuff from
here it is
"Waiting to connect to bluetoothd...\r\u001B[0;94m[bluetooth]\u001B[0m# \r\r\u001B[0;94m[bluetooth]\u001B[0m# \r \rAgent registered\n\u001B[0;94m[bluetooth]\u001B[0m# "
As you can see - the extracted "g" is from "Agent". No good...
I suspect Qt is messing with passing the backslash...
result = BTUL->EditLine_RegExp_Ext(result, "[/\w+/g]+",textEditPtr_DEBUG, textEditPtr_DEBUG);
<pre lang="C++">
"Waiting to connect to bluetoothd..."
"Waiting to connect to bluetoothd..."
"START EditLine_RegExp...QString BT_Utility_Library::EditLine_RegExp_Ext(QString, QString, QTextEdit *, QTextEdit *)1321"
" instring text \t\n Waiting to connect to bluetoothd..."
" regular expression \t\n [/w+/g]+"
" Has all match g"
|
|
|
|
|
The backslash character is used as the escape character within strings, so you need to escape it:
"[/\\w+/g]+"
|
|
|
|
|
Salvatore Terress wrote: Where is my error ??
Your question is not specific to regular expressions but also to what is running the regular expression engine. But you did not provide that information.
A 'g' is something that is external to regular expressions. So where you are using it is important and the only clue you provided is 'EditLine_RegExp' which googling for returned no results. But certainly since you didn't escape the backslash for the g that would never work.
Other than that of course there is also the following
- A space is not considered a word. Your capture group includes that.
- There could be more than one space.
- Are you matching on a single line? If not there are other complications.
- If there is ONLY words in your line then it is pointless to use regex at all. Just split it.
- If there are OTHER things besides words then I don't believe what you are doing will work (but again you didn't state what so maybe it is.)
|
|
|
|
|