Skip to content

Conversation

@hubert-reinterpretcast
Copy link
Contributor

No description provided.

Comment on lines +3723 to +3725
standard library. Whereas the \libheader{tgmath.h} macros can be suppressed in
C by, for example, using parentheses around the macro name, the \Cpp{} overload
sets cannot be bypassed in this manner; casts may be used instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems tutorial-like to be honest. The wording is clear that the tgmath.h macros don't exist in C++, and that's all that really needs to be said about compatibility differences.

Even if we want to have this new sentence, it seems like something that goes into a \begin{note} context.

I'm also not sure what you mean by "casts may be used instead". Are you casting the function itself to double(&)(double)? That isn't valid because of addressability restrictions. You could cast all the function arguments like sqrt((double)x), but that's possible in both languages, so it doesn't seem like it needs to be called out in [diff].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the statement about casts; @hubert-reinterpretcast , please tell us what you mean by those "casts".

I do want to point out that Annex C is tutorial-like in nature (nothing new in that Annex, hopefully), so I'm not objecting to adding a clarifying sentence. However, I wonder whether an actual code example would convey the consequences more clearly.

#include <tgmath.h>

double x = (sqrt)(2.0f);   // calls sqrt(double) in C, but sqrt(float) in C++

Maybe just striking "casts may be used instead" is a way forward?

@jensmaurer
Copy link
Member

@eisenwave , we didn't get a priority label here :-)

@eisenwave eisenwave added the P3-Other Triaged issue not in P1 or P2 label Dec 17, 2025
@eisenwave
Copy link
Member

How come all major compilers accept the following:

#include <cmath>
void f() {
    (std::sqrt)(0.0);
}

Wouldn't this need to be an error because std::sqrt is an lvalue that refers to an overload set, and parenthesized expressions presumably need to have a type?

@eisenwave eisenwave added P2-Bug Presentational errors and omissions and removed P3-Other Triaged issue not in P1 or P2 labels Dec 17, 2025
@eisenwave
Copy link
Member

eisenwave commented Dec 17, 2025

Hold on, I do think there is actually an incompatibility in some sense. I believe the wording currently mandates that if you include <math.h> you get the same stuff as <cmath>, minus things like std::lerp.

This means that the type of the expression sqrt(0.f) after including <math.h> in C++ is float, but in C, the type is double. In other words, the C++ behavior is always as if you had tgmath.h included, and there is no way to suppress it. I think that's what Hubert is aiming to address, even though the wording doesn't make it so obvious.

#include <math.h>
#include <type_traits>
// passes, but doesn't match the C behavior:
static_assert(std::is_same_v<decltype(sqrt(0.f)), float>);
// same:
static_assert(std::is_same_v<decltype((sqrt)(0.f)), float>);

So P2 because there is a missing incompatibility from [diff].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2-Bug Presentational errors and omissions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants