Confused on example in ScopedValueSetter docs – template args missing?

Minor one, just saw this code snippet as example in the docs

int x = 1;
 
{
    ScopedValueSetter setter (x, 2);
 
    // x is now 2
}
 
// x is now 1 again
 
{
    ScopedValueSetter setter (x, 3, 4);
 
    // x is now 3
}
 
// x is now 4

Isn’t this some C++ 17 magic? I guess in pre-C++ 17 the template type has to be explicitly specified like
ScopedValueSetter<int> setter (...) – or did I miss something? :wink: Might be confusing to newcomers that copy & paste the example code!

1 Like

This documentation hasn’t changed since it was introduced in January 2011 though… Maybe some old compilers were happy to deduce the template type from the arguments.

Interesting!

At least clang refuses to compile this code with C++ 14 as language standard and according to cppreference class template deduction through the constructor is C++ 17: https://en.cppreference.com/w/cpp/language/class_template_argument_deduction

1 Like