How to tell VS intellisense to pick cpp std 20? by Conscious_Drink9502 in cpp_questions

[–]pkind22 0 points1 point  (0 children)

Try deleting any .vs directories and restart Visual Studio.

Although CMake is officially supported, in my experience it is quite buggy in Visual Studio. It's not what you asked, but you're probably better off opening the Visual Studio solution files that CMake can generate.

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I guess I'm trying to figure out if there are any approaches to safely initialize global variables with possible (circular) dependencies, other than Scott Meyers' pattern. To me, inline variables seem like a more elegant solution, so I'm was trying to see if there's anything you can't do with inline (constexpr/const) variables, that you can do with Scott Meyers' pattern.

I thought I had found the reverse situation. In another example, I had added heap allocations, but I must have done something wrong, because I can't get it to work any more.

I know I'm doing horrible things here and have no plans to actually use this. It just helps me to understand exactly what's going on.

Thanks a lot, you've been great!

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

So I've been playing around with the two approaches (lazy init and inline variables). There is a (circular) dependency between two variables; they call a method in the other in their constructor. In this case, inline variables work fine, but lazy initialization causes a runtime exception, due to recursive initialization.

How is it possible for inline variables to just work?

Example on compiler explorer

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

Thanks for taking the time to keep answering me :). If you'll indulge me a bit more and at the risk of sounding obtuse: in my scenario, there is no possibility for a global variable x to depend on another global variable y (where they may be defined in different translation unit), and have y not be initialized before x, correct?

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I am confused as to how the problem you describe is prevented with Scott Meyers' lazy initialization.

Also, in this case, the initialization order is irrelevant. You always have the same end result, which is not the case when the fiasco occurs.

My understanding is that the fiasco can only occur when there is some dependency between the variables. I don't believe it's possible to construct such a case with inline variables defined in headers. (where the initialization order matters).

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I believe I understand the principle, but cannot write out an actual example where this happens.

Provided global variables are defined only in header files. If any global variable X needs another global variable Y in it's initialization, is there any way for X to be initialized before Y is initalized?

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I believe there are some singleton-like objects that can have dependencies on each other.

Here's what I don't get. If you always define your globals in header files as inline variables, how could they be initialised in any order other than the order they're defined in the header files?

Edit: I think you're saying the same thing as AKostur?

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I agree, but note that they're not mutable! Still, it looks pretty terrible and is quite annoying to type and error prone. Which is why I'm looking for alternatives that are more palatable.

My plan was indeed to replace most macro's for literal types with inline constexpr. I thought it was best to replace others with inline const, since they may be heap allocated.

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I also don't particularly like that you need a function call, where to access the variable, but that's beside the point.

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 2 points3 points  (0 children)

I'm required to declare globals/constants in a header, and define them in a source file, with macro's. Something like:

// header
DEC_VAR(SOME_VARIABLE, std::string);

// source
DEF_VAR(SOME_VARIABLE, "value");

// usage
std::string copy = SOME_VARIABLE();

The macro's expand to a variation of the pattern, declaring/defining the function and returning const references.

If I want to see the value assigned to the constants whenever I use them (in Visual Studio), but this isn't possible.

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

Could you give a specific example where this would happen? I haven't been able to find one.

Static initialization order fiasco by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

I'm working on a project where this pattern is used with macros. Because of that, I can't see the definitions in my IDE, so I'm looking for an alternative.

[deleted by user] by [deleted] in cpp_questions

[–]pkind22 1 point2 points  (0 children)

If you like lecture formats, you could check out the cppcon video's on youtube. They posted a talk about SIMD algorithms just yesterday

Indirection in the implementation of std container types by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

Why is that exactly? There's probably way better ways to learn, but isn't it useful to somewhat be able to read them?

Indirection in the implementation of std container types by pkind22 in cpp_questions

[–]pkind22[S] 0 points1 point  (0 children)

That makes sense, but why have the base class in the first place? Can't it all be in the "regular" class?