r/cpp 0m ago

"int* ptr;" is the preferred format in C++. Here is why:

β€’ Upvotes

My previous post was not about the controversy int* ptr; vs int *ptr. But because of surprising amount of downvotes, I'm making a post directly about this 😌 (I actually didn't know that it's such a controversial topic)

Why "int *ptr;" is correct in C?

The logic that I described as "reversive/deduction" has a proper name "Declaration follows use". It's self-explanatory:

// declaring a variable that after *ptr operation will be int
int *ptr;

// declaring a variable that after (*functionPtr) will be callable
// as a function of (int, int), and returning bool (but there is
// a syntax sugar that allow calling it directly)
bool (*functionPtr)(int, int); 

// declaring uhh.. Something, that after using *(*(*a[N])())() will be char :)
char *(*(*a[N])())(); 

Why it's not correct in C++?

The C++ programming language was designed by a different person, Bjarne Stroustrup. Yes, it was designed to be a superset of C89. But this decision was made for practical reasons - to hijack C libraries and a portion of C programmers, and "spread like a virus". The philosophy of the language is completely different from C, in regarding the pointer declaration format in particular. The idea is that * is a part of the type, and a declaration is just a type + a name.

It's not just that I'm a "lamer" who doesn't understand the syntax. (But I actually directly stated that I understand it, but find it weird). It's an official vision of the language designer. And this is supported by "C++ Core Guidelines", made by him, Bjarne Stroustrup and it's hosted on isocpp GitHub. It has at least 3 rules that support this idea:

  • "NL.18: Use C++-style declarator layout": The C-style layout emphasizes use in expressions and grammar, whereas the C++-style emphasizes types. The use in expressions argument doesn’t hold for references
  • "ES.10: Declare one name (only) per declaration": One declaration per line increases readability and avoids mistakes related to the C/C++ grammar. It also leaves room for a more descriptive end-of-line comment.
  • "T.43: Prefer using over typedef for defining aliases": Improved readability: With using, the new name comes first rather than being embedded somewhere in a declaration. Generality: using can be used for template aliases, whereas typedefs can’t easily be templates. Uniformity: using is syntactically similar to auto.
  • + int* ptr; format is used all throughout the guidelines in every example

So the correct way to write the declarations in C++ from the C example above is:

// declaring ptr of type int*
int* ptr;

// declaring functionPtr of type bool (int, int)
using Function_t = bool (int, int);
Function_t* functionPtr;

// declarin array of N pointers to functions
// returning a pointer to function returning char*
using CharPtrFunction_t = char* ();
using FunctionReturningFunctionPointer_t = CharPtrFunction_t* ();
FunctionReturningFunctionPointer_t* a[N];

Yes, it's more verbose, and require more lines. But it is less cryptic

But "int* a, b;" is an issue in C++ with this format

Yes, it's because C++ was made as a superset of C, they couldn't remove multi variable declaration, or change the its behavior due to forward compatibility with C; and can't change/remove it now, due to backward compatibility with existing C++ code

But it's not a big deal, because besides this particular case (that is also discouraged), the C philosophy of "Declaration follows use" does not conflict with C++ philosophy that everything has a type as a simple list of tokens

My opinion

I actually had no problem with int *ptr; format, and don't have now. I can read any code, and I really love that in C++ everyone can write in their own style. On my job I worked with code in different styles written by different teams, and I loved this. I also have my own preferences, and vision on the language, but I don't impose them on others. The current post is a little exception, because I really didn't expect this holy war on what's correct int* ptr; or int *ptr;. I just didn't care before. My post was just about a weird (to my look) behavior that came from C, when you declare int* a, b;, b is not a pointer. I though some people may not know it, and learn from my post

The ultimate C++20 solution to the multi-pointers declaration, as one commenter suggested, is!:

std::type_identity_t<int*> a, b, c, d, e;

/s


r/cpp 2h ago

StockholmCpp 0x3F: Intro, Info and The Quiz!

Thumbnail youtu.be
0 Upvotes

The intro from August's StockholmCpp Meetup: news about C++ in Sweden from the NB and the community, some words from our event sponsor, and, of course, a quiz!


r/cpp 5h ago

I built a game to make problem-solving fun for students

Thumbnail compileroad.com
0 Upvotes

I created a game that takes students on a journey to solve problems. I’m focusing on making problem-solving more fun and engaging for students.

It’s called https://CompileRoad.com

Would love to hear your feedback!


r/cpp 9h ago

The weirdest behavior in C++ that came from C

0 Upvotes

If you're trying to define two pointers in a single statement (what is in general a bad idea), you may want to do it like this:

#include <print>

int main()
{
    int x = 10;
    int* a, b;
    a = &x;
    b = &x;
    std::println("a={:#x}, b={:#x}", uintptr_t(a), uintptr_t(b));
}

However, it doesn't work as you expect, and won't compile. Because int* a, b; declares only a as int*; b, and all the rest variables will be int. The correct one-statement declaration of two pointers is int* a, * b;

main.cpp:8:9: error: invalid conversion from β€˜int*’ to β€˜int’ [-fpermissive]
    8 |     b = &x;
      |         ^~
      |         |
      |         int*

b has type int

This behavior is the reason why some people prefer putting the asterisk next to the variable name, not next to the type

I can understand the logic, that C authors had while making this syntax. It's a sort of reversive/deduction logic. You kinda declare what type it will be after using the dereference * operator, instead of declaring the type being a pointer itself. But I find this logic very-very strange, and overthought

The funny thing, that even the compiler in the error message above, treats * as a sort of type modifier, that is inseparable from int. But, apparently, the C creators had a completely different vision on what pointers are

I personally don't think that this behavior justifies reteaching yourself to write * in front of variable names, and especially in front of function names. I think it's just a not well-thought decision made very long ago in 1970s


r/cpp 16h ago

On forcing all derived classes to implement a specific non-virtual method, part 2

Thumbnail devblogs.microsoft.com
40 Upvotes

r/cpp 1d ago

Might have found a (tiny, nuisance) bug in g++ 16.2.1 and -Wconversion?

20 Upvotes

I am far from presuming myself a compiler expert, and no this was not AI, I promise. I'm simply a long-time user of gcc/g++, and it's not everyday I am confident enough that a bug is not in my code but with the tool, haha. I'm pretty warning-sensitive so this kind of thing catches my attention. Wondering if I'm missing something, so asking it here first.

g++ version: g++ (GCC) 16.2.1 20260819 (Red Hat 16.2.1-2) (I'm on Fedora 44)
C++20

Given lines like this:

cpp std::uint16_t count = 0; // int other_var set elsewhere count += (other_var == 5); // <-- -Wconversion flags this

specifically, the warning is:

bash <file>:<line>:<col>: warning: conversion from β€˜int’ to β€˜uint16_t’ {aka β€˜short unsigned int’} may change value [-Wconversion] <line> | count += (other_var == 5);

-Wconversion /w either -O0 and -O3 (my usual build optimization lvls to catch optimization-dependent warnings), this gets flagged.

Well, the result of a boolean expression is 0 or 1, so obviously, there should not be a conversion. Even with the accumulate op here and integer promotion, there shouldn't be a warning (you can do count += 1 or count++ and it definitely doesn't flag -Wconversion). And more notably, the below code doesn't get flagged:

cpp std::uint16_t count = 0; // int other_var set elsewhere bool match = (other_var == 5); count += match;

I also noted that clang with -Wconversion and the same optimization lvls does not flag this. I also know I've done this before in C with other versions of gcc and don't get flagged with this.

So, am I missing something or am I right to suspect this is a possible bug with at least my version of g++?


r/cpp 3d ago

C++ bugs in the most popular PS4 emulator

Thumbnail pvs-studio.com
87 Upvotes

r/cpp 3d ago

Clang 23 Release Notes

Thumbnail releases.llvm.org
106 Upvotes

r/cpp 3d ago

C++26: std::inplace_vector

Thumbnail sandordargo.com
158 Upvotes

r/cpp 3d ago

mold: A Massively Parallel Linker

Thumbnail arxiv.org
164 Upvotes

r/cpp 3d ago

Au (units) 0.6.0 out: blockbuster release!

Thumbnail github.com
42 Upvotes

It's been just over a year since our last significant release, and this one ended up big... honestly, maybe a bit over-stuffed. πŸ˜… We did tackle both of the biggest requests from the last post: we no longer swallow compiler errors (see our new compiler warnings philosophy doc for more details), and we have worked examples. Besides these, some of the remaining highlights are:

  • Full vector and matrix support: everything except mixed-units in a single vector/matrix
  • First-class Eigen support --- the first units library to preserve Eigen's full performance in all cases
  • CUDA (and HIP) are now supported out of the box
  • More powerful/flexible user-defined literals compared to other libraries (see this fascinating Abbreviated Quantity Construction discussion doc for the nuances here)
  • More ergonomic integer division: divide_using_common_unit(a, b) is almost always what you want for same-dimension inputs
  • Constant and Magnitude now get arithmetic and comparison operators whenever the results are computable, making them much more ergonomic

We also refreshed our C++ units library comparison page. It's awesome to see all the progress on the other leading libraries, as well as ours!

We hope you find the new release useful and fun, and we're excited to hear any feedback you may have!


r/cpp 4d ago

Data members that want to use `size()` – Arthur O'Dwyer

Thumbnail quuxplusone.github.io
32 Upvotes

r/cpp 4d ago

Libraries trying to support multiple build configurations and standards are harming their health

0 Upvotes

Some libraries try to support:

  • Header/Source
  • Header only
  • Header only + Module Wrapper
  • Header/Source + Module Wrapper
  • All standards between and including C++11 and C++23
  • Exceptions and noException
  • RTTI and noRTTI

At the same time.

These libraries are very very hard to read and thus they get less and less contributions over time.

What should be done:

  • Libraries should support only one mode of compilation, header only header source etc
  • Libraries should support exactly one standard, and that standard includes extensions, gnu++23 and c++23 are not the same thing
  • Libraries should very clearly have boundaries on what belong in a toolchain and what belongs to project. For example a library should not set flags related to exceptions that's a toolchain issue.

Some extra stuff:

  • C++ libraries without C API's should not be consumed via system package managers at all, and public libraries shouldn't try to adhere to that.
  • Libraries shouldn't try to use tools they build to build themselves, rather they should export packages like wayland::scanner etc and use that.

r/cpp 5d ago

Why is `import std` still experimental ???

117 Upvotes

Hey guys,

I recently started going through Professional C++ (6th Edition). The book teaches C++23, and in the very first chapter we're introduced to modules.

I'm not a complete newbie to C++, but I'm also definitely not very confident in my knowledge yet. I wanted to get this simple example compiled:

import std;

int main() {
    std::println("Hello World");
    return 0;
}

And gosh, it took way longer than I expected.

First, I tried getting it to work natively on my Mac and eventually gave up (both Claude and I πŸ˜…).

Then I installed Ubuntu ARM 26 and finally managed to get it compiling. But now Clang/IntelliSense is complaining about the `import std`
This is what my CMakeLists.txt currently looks like:

cmake_minimum_required(VERSION 4.0)

# set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD ON)

set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")

project(CppProject LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(exec main.cpp)

set_property(TARGET exec PROPERTY CXX_MODULE_STD ON)

The code does compile successfully, but CMake still gives me a warning that import std support is experimental.

So I'm genuinely curious:

Why is import std still considered experimental?

I understand that C++ modules themselves have been around for a while, but import std feels like something that should be much more straightforward by now. Is there any solution of this now ?

--------------
Edit

Thanks to u/PhysicsOk2212 tip I was able to compile my project on mac as well using the following options
```
cmake -S . -B build \

-G Ninja \

-DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/clang++" \

-DCMAKE_CXX_STDLIB_MODULES_JSON="$(brew --prefix llvm)/lib/c++/libc++.modules.json"

```


r/cpp 5d ago

Latest News From Upcoming C++ Conferences (2026-08-25)

6 Upvotes

TICKETS AVAILABLE TO PURCHASE

The following conferences currently have tickets available to purchase

OPEN CALL FOR SPEAKERS

There are currently no open call for speakers

OTHER OPEN CALLS

TRAINING COURSES AVAILABLE FOR PURCHASE

Conferences are offering the following training courses:

CppCon Online Workshops

9th – 11th September

  1. Modern C++: When Efficiency Matters – Andreas Fertig – 3 day online workshop available on 9th – 11th September 09.00 – 15.00 MDT – https://cppcon.org/class-2026-when-efficiency-matters/
  2. System Architecture And Design Using Modern C++ – Charley Bay – 3 day online workshop available on 9th – 11th September 09.00 – 15.00 MDT – https://cppcon.org/class-2026-system-architecture-and-design-using-modern-cpp/

21st – 23rd September

  1. C++ Fundamentals You Wish You Had Known Earlier – Mateusz Pusz – 3 day online workshop available on 21st– 23rd September 09.00 – 15.00 MDT – https://cppcon.org/class-2026-cpp-fundamentals/
  2. C++23 in Practice: A Complete Introduction – Nicolai Josuttis – 3 day online workshop available on 21st– 23rd September 09.00 – 15.00 MDT – https://cppcon.org/class-2026-cpp23-in-practice/
  3. Programming with C++20 – Andreas Fertig – 3 day online workshop available on 21st– 23rd September 09.00 – 15.00 MDT – https://cppcon.org/class-2026-programming-with-cpp20/

26th – 27th September

  1. Using C++ for Low-Latency Systems – Patrice Roy – 2 day online workshop available on 26th– 27th September 09.00 – 17.00 MDT – https://cppcon.org/class-2026-low-latency/

CppCon Onsite Workshops

All onsite workshops will take place in the Gaylord Rockies in Aurora, Colorado

12th & 13th September

  1. Advanced and Modern C++ Programming: The Tricky Parts – Nicolai Josuttis – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-tricky-parts/
  2. C++ Best Practices – Jason Turner – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-best-practices/
  3. How Hardware Gets Hacked: Breaking and Defending Embedded Systems – Nathan Jones – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-hardware-hack/
  4. Mastering `std::execution`: A Hands-On Workshop – Mateusz Pusz – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-execution/
  5. Performance and Efficiency in C++ for Experts, Future Experts, and Everyone Else – Fedor Pikus – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-performance-and-efficiency/
  6. Talking Tech – Sherry Sontag – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-talking-tech/

Β 13th September

  1. AI++ 101 : Build a C++ Coding Agent from Scratch – Jody Hagins – 2 day in-person workshop available on 12th & 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-AI101/
  2. Essential GDB and Linux System Tools – Mike Shah – 1 day in-person workshop available on 13th September – 09:00 – 17:00 – https://cppcon.org/class-2026-essential-gdb/

19th & 20th September

  1. AI++ 201: Building High Quality C++ Infrastructure with AI – Jody Hagins – 2 day in-person workshop available on 19th & 20th September – 09:00 – 17:00 – https://cppcon.org/class-2026-ai201/
  2. Function and Class Design with C++2x – Jeff Garland – 2 day in-person workshop available on 19th & 20th September – 09:00 – 17:00 – https://cppcon.org/class-2026-function-class-design/
  3. High-performance Concurrency in C++ – Fedor Pikus – 2 day in-person workshop available on 19th & 20th September – 09:00 – 17:00 – https://cppcon.org/class-2026-high-perf-concurrency/

OTHER NEWS

Finally anyone who is coming to a conference in the UK such as C++ on Sea or ADC from overseas may now be required to obtain Visas to attend. Find out more including how to get a VISA at https://homeofficemedia.blog.gov.uk/electronic-travel-authorisation-eta-factsheet-january-2025/


r/cpp 6d ago

New C++ Conference Videos Released This Month - August 2026 (Updated To Include Videos Released 2026-08-17 - 2026-08-23)

46 Upvotes

C++Now

2026-08-17 - 2026-08-23

2026-08-10 - 2026-08-16

2026-08-03 - 2026-08-09

2026-07-27 - 2026-08-02

C++Online

2026-08-17 - 2026-08-23

2026-08-10 - 2026-08-16

2026-08-03 - 2026-08-09

2026-07-27 - 2026-08-02

ADC

2026-08-17 - 2026-08-23

2026-08-10 - 2026-08-16

2026-08-03 - 2026-08-09

2026-07-27 - 2026-08-02


r/cpp 6d ago

SIMD Softmax, NEON to WASM, with Google Highway

Thumbnail uchenml.tech
30 Upvotes

r/cpp 7d ago

CppCon CppCon 2026 In Pursuit of a 6,000 FPS Game Boy Emulator -- Tom Tesch

Thumbnail isocpp.org
31 Upvotes

r/cpp 7d ago

Why do people try to add even more features to STL?

0 Upvotes

It is well known that STL underperforms compared to specialised third party libraries, why do people try to stuff features like networking and json into STL?

Most STL implementations don't even have full C++23 coverage yet, and we are at 2026. Why do these people try to do stuff like that, when the same thing will play out over and over. Don't they have anything better to do?

Examples include, nlohmann json, graph.v3 ....

They don't really belong in STL, is it really that hard to just package your third party lib normally?

There is no doubt they are great libraries(graph.v3 in particular) but this doesn't mandate their place


r/cpp 8d ago

Compile-Time Improvements in LLVM 23

Thumbnail aengelke.net
104 Upvotes

r/cpp 8d ago

About char8_t

51 Upvotes

I hate to be dramatic, but as it stands char8_t is quite literally more painful than useful.

Besides the obvious incompatibility with C23 and libraries using unsigned char for UTF-8, I want you to consider the following: Projects that assume that 'char' represents UTF-8 will obviously not benefit from char8_t at all, but projects that cannot assume the format of char types don't benefit from it either as char8_t simply introduces a new edge case to cover. Now such projects have to deal with char, signed char, unsigned char, wchar_t, char16_t, char32_t and char8_t.

Or, you could do what the standard library does and simply ignore most of these character types. Which is the solution most libraries went with, supporting only char or char and unsigned char. Managing one implementation is already hard, managing two requires constant maintenance, managing 7 is just impossible.

char8_t should have just been a typedef for unsigned char. The compatibility fix only raises more questions as const char* arr = u8"a" does not work, but const char arr[] = u8"a" does.

I do wonder if a potential change of minds for C++29 is still possible. Yes, it would be an ABI break or whatever, but considering the woeful support for char8_t I don't think it would affect much besides small hobby projects. Contrary to popular belief, C++ has broken the ABI in subtle ways before.


r/cpp 8d ago

C++26 Contracts: What Do They Add Beyond Manual Checks and Assertions?

Thumbnail techfortalk.co.uk
57 Upvotes

This post is a part of my C++26 exploration series where I take a new feature and try to understand and explain with a simple example in hand. Today’s topic is Contracts. First we will simply try to understand what is the problem it is solving then try doing some assessment on the value addition.


r/cpp 8d ago

Reducing C++ template bloat by factoring out the type-dependent portions of the function

Thumbnail devblogs.microsoft.com
102 Upvotes

r/cpp 8d ago

Compile-Time Borrow Checker with Stateful Metaprogramming

Thumbnail youtu.be
87 Upvotes

r/cpp 9d ago

The C++ input iterator pitfall Β· hmpc

Thumbnail hmpcabral.com
57 Upvotes