# C++ interop type mapping for integer and floating-point literals
# Table of contents
# Overview
The behavior of literals that participate in interop depends on the direction of interop, but generally follows C++ rules:
- Carbon literal to C++ type: A Carbon literal will be given a C++ type according to the C++ rules when used in C++ context, such as calling a C++ function.
- C++ literal to Carbon type: A C++ literal will be given a C++ type
following the C++ rules, which will then be mapped to a Carbon type as
defined in
primitive types mapping.
- For example,
1becomesintaccording to C++ rules, andintmaps toi32in Carbon.
- For example,
This is used:
- During overload resolution of C++ functions, when a C++ function is called from Carbon with a literal as an argument.
- When importing C++ macros to Carbon, such as macros defining constants where the constant is a literal.
# Carbon to C++ type mapping
# Integer literals
Because there are no suffixes in Carbon for the integer types, a Carbon decimal integer literal will follow the C++ rules for decimal integers. Carbon also doesn't make a distinction between hexadecimal and binary literals for interop, so these literals will also follow the C++ rules for decimal integers without a suffix.
The first type from the following list in which the literal's value can fit is selected:
intlonglong long__int128
If the value doesn’t fit in any of these types, the program is ill-formed and
diagnosed with an error. This matches the C++ standard behavior rather than
Clang's -Wimplicitly-unsigned-literal behavior.
Because decimal numbers are most commonly used as integer literals, this should match most existing C++ calls. To match a C++ call with a non-decimal literal argument expecting an unsigned type, an explicit unsigned type must be provided in Carbon. For example:
Cpp.f(0xDEADBEEF as u32);
# Floating-point literals
As there are no suffixes in Carbon for the floating-point literals, a Carbon
floating-point literal will map to C++ double. If the value doesn’t fit in
double, the program is ill-formed.
# C++ to Carbon type mapping
# Integer literals
A C++ integer literal will be given a C++ integer type following the C++ rules, which is then mapped to a Carbon type as defined in the primitive types mapping.
# Floating-point literals
A C++ floating literal will be given a C++ type following the C++ rules, which is then mapped to a Carbon type as defined in primitive types mapping. :
# Alternatives considered
Carbon to C++ type mapping
- Use the C++ standard rules for hexadecimal and binary literals
- Use the Clang way of determining the type
C++ to Carbon type mapping