-2

I've searched many resources and in the examples that they provide, it looks like this:

void bar(int a = 4, char b = 'A') 
{ 
}

According to this resource it can be called like:

bar(b : 'Z');

but it gives me an error identifier "b" is undefined. I am using Visual Studio 2015 Update 3 Enterprise.

Any suggestions how to specify only the second parameter?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
mihkov
  • 1,171
  • 13
  • 37
  • By the way, this proposal probably will never be implemented (as far as I can tell), because it clashes with the technicalities of how C++ functions are called, and makes offering linkable libraries incompatible to anything that's out there. – Marcus Müller Aug 21 '16 at 17:05
  • Ok, will be closed – mihkov Aug 21 '16 at 17:12
  • @MarcusMüller Tbh I don't quite see how that makes the question off-topic. – Baum mit Augen Aug 21 '16 at 17:14
  • @BaummitAugen well, we close questions as off-topic that just exist because the author had a typo in their code. Misunderstanding the role of something that is a proposal is pretty much the same; it can be fixed by pointing the author to the mistake, which is of a low-level "lexing" nature, and doesn't hold future value for other visitors (unless the misunderstanding/typo is likely to happen to someone else). – Marcus Müller Aug 21 '16 at 17:19
  • FWIW, the best I've seen right now that actually works uses `bar("b"_arg = 'Z')` syntax, but I believe it uses a non-standard language feature that GCC and Clang support. – chris Aug 21 '16 at 17:23
  • @MarcusMüller I don't agree with that. Not knowing what a C++ standards proposal paper is feels like a legitimate mistake to make and, unlike a typo mistake, can neither be solved by the author staring at the code nor reiterated in infinitely many near-equivalent variants. So even if that was the core of the question, it may be a dupe / downvotable for lack of research effort, but off-topic by analogy to typo seems like a stretch. – Baum mit Augen Aug 21 '16 at 17:26
  • Hm, I can see that point; I'll retract my close vote. – Marcus Müller Aug 21 '16 at 17:27

2 Answers2

7

The paper you link is just a proposal¹. The mechanism you try to use is (currently) not a part of C++ as defined in the standards. You can thus not expect compilers to implement it.

There are currently no other standard workarounds. If you want to set some parameter, you also need to provide all parameters that precede it in the function declaration.

You can use something like Boost Parameter or implement something similar yourself to emulate the feature with user code. Whether or not that is a good idea is up to debate, it is not all too common and may thus be confusing for other programmers.


¹This might be a language problem: A "proposal" is something that someone says would be good, not the state things are.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
0

One possible solution might be to use std::bind with the placeholders defined in that namespace. For example:

void bar(int a = 4, char b = 'A') {
}

using namespace std::placeholders;
auto f = std::bind(bar, 2, _2);
f('Z'); // equivalent to bar(2, 'Z');

As suggested in the other answer, there's no way to have the std::bind expression use the default argument from the function declaration, but it at least allows you specify only one of the arguments to it.

bnaecker
  • 6,152
  • 1
  • 20
  • 33