llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.37k stars 12.14k forks source link

[feature request] add reference to clang as an extension #116862

Open olemayu opened 1 week ago

olemayu commented 1 week ago

I'd like to propose adding reference to C as an clang extension. The same one C++ has.

A reference, like a pointer, stores the address of an object that is located elsewhere in memory. Unlike a pointer, a reference after it's initialized can't be made to refer to a different object or set to null. There are two kinds of references: lvalue references, which refer to a named variable and rvalue references, which refer to a temporary object. The & operator signifies an lvalue reference and the && operator signifies either an rvalue reference, or a universal reference (either rvalue or lvalue) depending on the context. https://learn.microsoft.com/en-us/cpp/cpp/references-cpp

int main() {
   int i; // Declare the object.
   int& ref = i; // Declare and initialize the reference.

   ref = 5;

   printf("%i\n", i); // "5"
}
AaronBallman commented 1 week ago

This would be a rather ambitious extension in Clang and it steps on the committee's design space for the C language. In order to proceed with such a feature, I think we'd need a paper in front of WG14 asking for their opinions to see if there's a path towards standardization (part of our usual criteria for extensions). Based on my time on the committee, I suspect it will be a hard sell.

llvmbot commented 1 week ago

@llvm/issue-subscribers-c

Author: None (olemayu)

I'd like to propose adding reference to C as an clang extension. The same one C++ has. > A reference, like a pointer, stores the address of an object that is located elsewhere in memory. Unlike a pointer, a reference after it's initialized can't be made to refer to a different object or set to null. There are two kinds of references: *lvalue* references, which refer to a named variable and *rvalue* references, which refer to a [temporary object](https://learn.microsoft.com/en-us/cpp/cpp/temporary-objects?view=msvc-170). The `&` operator signifies an lvalue reference and the `&&` operator signifies either an rvalue reference, or a universal reference (either rvalue or lvalue) depending on the context. > https://learn.microsoft.com/en-us/cpp/cpp/references-cpp ```c int main() { int i; // Declare the object. int& ref = i; // Declare and initialize the reference. ref = 5; printf("%i\n", i); // "5" } ```