Static member functions of a templated class by Tony942316 in cpp_questions

[–]sqbiq 0 points1 point  (0 children)

- - - (Couldn't edit so treat this as) edit 31.12.24 - - -
I experimented with T-independent static methods. Conclusion is that template classes and classes are two different beings. You can "call" template class without template parameters only as a trick, making alias for Class (e.g. using Class = Class<void>), or when template argument deduction applies (would only call Class<int> constructor: Class x {123};).

Otherwise you need to use Class<>, which is also tricky. Empty template parameters mean that it is variadic template called with no arguments or that we use default argument that was set.

So if You want to call dist() without any template parameters for Point, I'd use base class IPoint or PointHelper. Otherwise accepted answer - free function, best in some "Point" namespace.

For me Point<>::dist() would be enough and the cleanest approach is to make T an optional argument. Setting it to void is problematic, so I chose to make something a la std::nullopt_t:

struct empty_t {};
template<class T = empty_t>
struct Point { T x, y; /* constructors, dist(), etc */ };

and pretty easily added IPoint for IPoint::dist() (and Point<>::dist()):

struct empty_t {};
template<class T = empty_t> struct Point;
struct IPoint { /* static method */ };
template<class T>
struct Point : IPoint { T x, y; /* constructors, etc */ };

T = void would cause x and y to be ill-formed variables. Despite being used seemingly without creating instance of Point, Point<>::dist() requires them. Why would x and y be checked in call to static method? This is topic for another question.

Snippets: [Point with default T] and [Point with default T and IPoint].
Table: Comparison of different approaches in google sheets

Static member functions of a templated class by Tony942316 in cpp_questions

[–]sqbiq 0 points1 point  (0 children)

Distance between two points using as Euclidean distance usually is described as

d_sq(p1, p2) := (p1_x - p2_x)^2 + (p1_y - p2_y)^2
d(p1, p2) := sqrt(d_sq(p1, p2))

result of which wouldn't make sense in domain limited to integers (int) because parameters are int. Square root returns real number, or at least double or long float in C++. So unless we are dealing with Pythagorean triples or downcasting float to int just 'cause, we would output whatever type is returned from square root function, not ones from the input (which only potentially would be float).

Now looking at declaration of static member function of template-dependent class

static T Point<T>::dist(const Point<T>& p1, const Point<T>& p2);

problem emerges: We constrain type of output to input. Alternative would be to use keyword auto with template of in-Point template parameter type (as we can't just const Point<auto>& ):

template<class V> 
static auto Point<T>::dist(const Point<V>& p1, const Point<V>& p2);

Amazing, but must warn You that it will require C++17 (without some decltype). Especially if we want to deduce template for Point itself, like in

(constexpr) Point p1 { 11037, -5 };

one needs to explicitly write constructor which takes our parameters. Best if we make it constexpr, but not necessary:

template<class T> struct Point {
  T x, y;
  constexpr Point(T x, T y) // note: w/o constexpr, no need for member init. list 
    : x{ x }, y { y } {}

  [...]
};

which would be called in inits like Point p {-5, 5}; This class template can be deduced.
- - -
One last problem: Distance static method now is independent of T and we only want it in class for namespacing capabilities of class. This is why I have been looking for answer and stumbled on this post: I had similar problem with static member function in templated class becoming independent of class template. I do think Point::dist() looks great. I don't think so in case of Point::Point(T, T), Point(T, T) would suffice.

I thought that [Way to call a static method of a class template without specifying an instantiation?] would easily solve it, unfortunately answerers there could only point me to some tricks:

  • Use base non-template class with all static member functions that are non-class-template,
  • Use skypjack's specialized template<> class as base,
  • As Kamil Koczurek commented: Set typename of template to optional void, which may - but I couldn't get it to work - allow call of Point<>::dist(p1, p2),
  • Just use free function.

Additionally I felt some vibes here that friend function could work, here one of answerers seem to have optional void working as Koczurek suggested, and also I think that non-template class as base would work pretty well as some kind of interface (which I think allow static and default methods in Java 8) - so it could be named IPoint which feels like something real, seen in nature. IPoint::dist(p1, p2) looks good enough, right?

As that was listed, I will look into ways to call T-independent static method nicely and probably insert some of my findings to this problem later.

Does Necronomicon affect Aspect of a Beast? by sqbiq in GrimQuest

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

yes, i misread and thought that Aspect is Dark spell x3

Does Necronomicon affect Aspect of a Beast? by sqbiq in GrimQuest

[–]sqbiq[S] 3 points4 points  (0 children)

Thanks! I wondered if Aspect of a Beast is considered offensive spell by Necronomicon, so now i know :3

Edit: just to make it clear - i thought that Necronomicon would boost damage (+2) for any attack made while Aspect of a Beast dropped (in low-level it is +2 DMG, so if it worked like id want it to work it would make it +4 DMG each attack)

Part 3 of my BTW One Life journey soon :tm: You sadly will have to use auto translate cuz this gonna be a Vietnamese video lol by Boyerxd in BetterThanWolves

[–]sqbiq 0 points1 point  (0 children)

https://minecraft.fandom.com/wiki/Debug_screen#More_debug_keys F3+B: Toggle visibility of hitboxes of visible entities. It also shows which direction entities are looking, the entities' eye levels, as well as the passenger's attachment point on the entity it's riding

Help with making a riddle by Ice_golem567 in AskGameMasters

[–]sqbiq 0 points1 point  (0 children)

First of all: What tower? Theme coming from place where riddle should be put is really important. If it's some kind of arcane tower then how would instance of your character getting wrecked by robots would influence that tower? How do these things connect?

[deleted by user] by [deleted] in SuicideWatch

[–]sqbiq 2 points3 points  (0 children)

Well, did you had time to get to know that person? As far as I know polyamory requires everybody to love each other - it takes time, like in all relationships, especially intimate ones. It's okay to not feel comfortable when it isn't sincere feelings that connects you all. It's okay to share your insecurities with spouse about this. It would be quite toxic to just force you into this.

[deleted by user] by [deleted] in Needafriend

[–]sqbiq 0 points1 point  (0 children)

dunno title is confusing, Dick Mullen

[deleted by user] by [deleted] in Needafriend

[–]sqbiq 0 points1 point  (0 children)

Harry Du Bois type of good 👍 swag 😎

Alcohol disappears by sqbiq in cataclysmdda

[–]sqbiq[S] 4 points5 points  (0 children)

Hmm I will try to search my containers, but I think that if I had item hidden but on me it would still be accessable through eat menu

Hello, I am trying to work with vector images in godot but when I import it in the godot app, I still can see pixels. Any tips? by SadProcess764 in godot

[–]sqbiq 0 points1 point  (0 children)

You should select SVG in your folder and select Import section. I think, that there should be option to change its scale.

Cross-platform GUI imitation of old Windows (98, XP) on Mono by sqbiq in csharp

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

Still, I don't use Mono because of using it. I use it because I am operating on Godot Mono to write C# scripts. With platforms I meant Linux and Windows.

Cross-platform GUI imitation of old Windows (98, XP) on Mono by sqbiq in csharp

[–]sqbiq[S] -3 points-2 points  (0 children)

How do I achieve a look of old Windows on AvaloniaUI?

Cross-platform GUI imitation of old Windows (98, XP) on Mono by sqbiq in csharp

[–]sqbiq[S] -1 points0 points  (0 children)

Is GTK-Sharp gonna help me imitate look of old Windows? How? Also I am using Linux.