Issue |
143469
|
Summary |
Compiler extension for Rust-like using syntax
|
Labels |
new issue
|
Assignees |
|
Reporter |
mikomikotaishi
|
I do not know if requests/suggestions for compiler extensions are accepted, but I would like to suggest adding Rust-style `using` statements as a language extension for Clang. What I mean by that is, in Rust the `use` statement has much more versatility and ergonomics than in C++. Example:
```rust
use std::env::Args;
use std::fs::{File, FileType};
use std::io;
use std::os::{linux::{fs::MetadataExt, process::PidFd}, unix::net::Incoming, windows::{io::BorrowedSocket, ffi::EncodeWide}};
use std::thread::*;
```
If we wanted to load the same symbols into scope in C++, the equivalent code in ISO C++ (if C++ had the same standard library structure as Rust), would be:
```cpp
using std::env::Args;
using std::fs::File;
using std::fs::FileType;
namespace io = std::io;
using std::os::linux::fs::MetadataExt;
using std::os::linux::process::PidFd;
using std::os::unix::net::Incoming;
using std::os::windows::io::BorrowedSocket;
using std::os::windows::ffi::EncodeWide;
using namespace std::thread;
```
The proposed syntax in C++ (again assuming the C++ standard library was equivalent to the Rust standard library), would instead be:
```rust
using std::env::Args;
using std::fs::{File, FileType};
using std::io;
using std::os::{linux::{fs::MetadataExt, process::PidFd}, unix::net::Incoming, windows::{io::BorrowedSocket, ffi::EncodeWide}};
using std::thread::*;
```
For instance, if we were using the actual C++ standard library, we could instead write `using std::chrono::*;` instead of `using namespace std::chrono;`. Or, instead being able to write `using std::ranges;` to do:
```cpp
using std::ranges;
// We can write ranges::min() instead of std::ranges::min()
int minimum = ranges::min({33, 54, 13, 802, 7, 61});
```
Again, I do not know if this is an appropriate place to request/suggest compiler/language extensions, but such a feature would be greatly beneficial for improving ergonomics especially as `using` statements are much more viable with the addition of modules to C++, which do not export `using` statements by default unlike headers.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs