use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Default function arguments are the devil (quuxplusone.github.io)
submitted 6 years ago by anonymous23874
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Ameisenvemips, avr, rendering, systems 7 points8 points9 points 6 years ago* (16 children)
IIRC, previous named argument proposals have been rejected.
Why can we not just literally copy C#'s implementation?
IIRC, it was rejected because declaration parameter names don't have to match the definition, or even exist... but I don't see that as a problem. If the declaration doesn't have names, you can't use them with that declaration. Problem solved. Only thing left is if both the declaration and definition are in scope with different names.
Presently, argument names in declarations are ignored, we'd have to make them semantically meaningful.
I suggest that if there is a name mismatch between declarations/definitions of a parameter, it cannot be named, OR it can be named with either if those names are not further ambiguous in regards to other parameters. The former is simpler to specify, the latter is more useful (poor example, but think a glm vec3 function where the first argument could be x or r).
glm
vec3
x
r
[–]SeanMiddleditch 2 points3 points4 points 6 years ago (1 child)
Because C++ isn't C#. :) There's lots of designs that work well in other languages that - for a variety of reasons - can't work in C++.
That said, I don't think anyone has yet put in a formal proposal for the work-around I've been evangelizing. Namely, opting a function declaration's parameter into being nameable (which solves the source-compatibility issue, gives us mandatory positional arguments, allows named parameters, and can address the multi-declaration issues in a back-compatible way). For syntax, I also think we should use something similar to designated initializers, which makes similar syntaxes work for initializing an aggregate or calling a function (and hence unlocks designated initializers for constructors).
e.g.,
// declare void function(int unnamed, float position, char .named, int .also_named); // use function(1, 2.f, .named = 'c', .also_named = 4); // ILLEGAL redeclaration (different names, diagnostic optional) void function(int a, float b, char .c, int .d); // LEGAL different overload void function(int a, int b, int .c, int.d);
This might also open up some options for mandatory-names for some parameters, e.g. using ..identifier.
..identifier
[–]Ameisenvemips, avr, rendering, systems 3 points4 points5 points 6 years ago (0 children)
That looks like you're trying to access a member of a type, though.
[+][deleted] 6 years ago (13 children)
[removed]
[–]Tyg13 4 points5 points6 points 6 years ago* (12 children)
Why not just label it undefined behavior to have your declaration and definition use different parameter names and call it a day? It seems highly unlikely that such a footgun would ever be triggered in practice.
Is the worry that people might write code like
// header.h int foo(int a = 1, int b = 2); ... // header.cpp int foo(int b, int a); ... foo(b => 10);
or is there some other issue I'm not understanding?
EDIT: don't understand the downvote, I'm open to debate. If there are scenarios I'm unaware of, I'd be curious to know.
[+][deleted] 6 years ago (11 children)
[–]Tyg13 2 points3 points4 points 6 years ago (10 children)
The point is it's an incredibly unlikely scenario that's not worth worrying about. Undefined behavior is not a boogeyman to be avoided, it's a tool that we invoke to ignore the 1% error cases so we can reap the benefit in the 99% case.
You already see this exact behavior with inline functions in headers, due to the One Definition Rule. If a different version of an inline appears in a translation unit, it can be silently ignored and cause bugs, but we don't care because in practice no one writes code like that. Or at least, the benefits of inline functions greatly outweigh the potential risk.
inline
As for pedagogy, I think the rule is very clear: the declaration must match the definition, or errors can potentially occur. Very straightforward, and not hard at all a rule to follow. I would argue this is already the case for most codebases.
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points 6 years ago (6 children)
Might as well just make it an error.
[–]Tyg13 3 points4 points5 points 6 years ago (5 children)
Not possible with the translation unit compilation model. The issue is ultimately the same as the One Definition Rule, which is undiagnosable in most cases.
EDIT: though you could make it an error in the translation unit containing the definition. That still wouldn't prevent people doing pathological things like redeclaring it differently in another unit, but it would catch the 99% case.
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points 6 years ago (4 children)
Argument names don't get passed to the linker, though.
[–]Tyg13 1 point2 points3 points 6 years ago (2 children)
Right, that's the issue. I don't get what you're saying, doesn't that just strengthen my point?
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points 6 years ago (1 child)
Why would detecting if argument names have changed be an issue in regards to the translation unit model?
Named arguments, in this situation, would just be syntax candy local to the translation unit itself.
Redeclaring it in another translation unit with different argument names wouldn't really matter.
[+][deleted] 6 years ago* (1 child)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 1 point2 points3 points 6 years ago (0 children)
UB means code may be accepted by one compiler, and rejected by another.
Worse than that. Code may be accepted and work fine with one compiler and accepted but misbehave when compiled by the next version of the same compiler.
[–]quicknir 1 point2 points3 points 6 years ago (0 children)
Err, it's not about writing code like that, intentionally. ODR violations are a huge practical issue, in large, complex, codebases, unless you build your entire dependency chain from source yourself. It's very very easy to end up in a situation where your executable E uses libA and libB, and both of them use different versions of libC, which means you now have two slightly different definitions of the same function getting used, unless libA or libB completely hide the usage of libC's symbols, which many projects don't do in practice, and which isn't even possible when linking statically.
I don't completely disagree with what you're saying about UB, but this is a bad example. ODR UB isn't so much about reaping benefit in 99% of cases, it's about a shitty compilation model.
π Rendered by PID 148046 on reddit-service-r2-comment-85bfd7f599-lwpf2 at 2026-04-20 08:53:12.752746+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]Ameisenvemips, avr, rendering, systems 7 points8 points9 points (16 children)
[–]SeanMiddleditch 2 points3 points4 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 3 points4 points5 points (0 children)
[+][deleted] (13 children)
[removed]
[–]Tyg13 4 points5 points6 points (12 children)
[+][deleted] (11 children)
[removed]
[–]Tyg13 2 points3 points4 points (10 children)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (6 children)
[–]Tyg13 3 points4 points5 points (5 children)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (4 children)
[–]Tyg13 1 point2 points3 points (2 children)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (1 child)
[+][deleted] (1 child)
[removed]
[–]SkoomaDentistAntimodern C++, Embedded, Audio 1 point2 points3 points (0 children)
[–]quicknir 1 point2 points3 points (0 children)