The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter.
Here's an example class with a constructor that can be used for implicit conversions:
class Foo
{
public:
// single parameter constructor, can be used as an implicit conversion
Foo (int foo) : m_foo (foo)
{
}
int GetFoo () { return m_foo; }
private:
int m_foo;
};
Here's a simple function that takes a Foo
object:
void DoBar (Foo foo)
{
int i = foo.GetFoo ();
}
and here's where the DoBar
function is called:
int main ()
{
DoBar (42);
}
The argument is not a Foo
object, but an int
. However, there exists a constructor for Foo
that takes an int
so this constructor can be used to convert the parameter to the correct type.
The compiler is allowed to do this once for each parameter.
Prefixing the explicit
keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42)
. It is now necessary to call for conversion explicitly with DoBar (Foo (42))
The reason you might want to do this is to avoid accidental construction that can hide bugs.
Contrived example:
- You have a
MyString
class with a constructor that constructs a string of the given size. You have a function print(const MyString&)
(as well as an overload print (char *string)
), and you call print(3)
(when you actually intended to call print("3")
). You expect it to print "3", but it prints an empty string of length 3 instead.
Best Answer
Language features:
Templates and Generic Code
Template argument deduction for class templates
template <auto>
Non-type template arguments fixes
template<template<class...>typename bob> struct foo {}
( Folding + ... + expressions ) and Revisions
auto x{8};
is anint
modernizing
using
with...
and listsLambda
constexpr lambdas
Capturing
*this
in lambdas[*this]{ std::cout << could << " be " << useful << '\n'; }
Attributes
[[fallthrough]]
,[[nodiscard]]
,[[maybe_unused]]
attributes[[attributes]]
onnamespace
s andenum { erator[[s]] }
using
in attributes to avoid having to repeat an attribute namespace.Compilers are now required to ignore non-standard attributes they don't recognize.
Syntax cleanup
Inline variables
namespace A::B
Simple
static_assert(expression);
with no stringno
throw
unlessthrow()
, andthrow()
isnoexcept(true)
.Cleaner multi-return and flow control
Structured bindings
std::tie
withauto
const auto [it, inserted] = map.insert( {"foo", bar} );
it
andinserted
with deduced type from thepair
thatmap::insert
returns.std::array
s and relatively flat structsif (init; condition)
andswitch (init; condition)
if (const auto [it, inserted] = map.insert( {"foo", bar} ); inserted)
if(decl)
to cases wheredecl
isn't convertible-to-bool sensibly.Generalizing range-based for loops
if constexpr
Misc
Hexadecimal float point literals
Dynamic memory allocation for over-aligned data
Guaranteed copy elision
Fixed order-of-evaluation for (some) expressions with some modifications
.then
on future work.Direct list-initialization of enums
Forward progress guarantees (FPG) (also, FPGs for parallel algorithms)
u8'U', u8'T', u8'F', u8'8'
character literals (string already existed)"noexcept" in the type system
__has_include
Arrays of pointer conversion fixes
inherited constructors fixes to some corner cases (see P0136R0 for examples of behavior changes)
aggregate initialization with inheritance.
std::launder
, type punning, etcLibrary additions:
Data types
std::variant<Ts...>
std::optional
std::any
std::string_view
std::string
like reference-to-character-array or substringstring const&
again. Also can make parsing a bajillion times faster."hello world"sv
char_traits
std::byte
off more than they could chew.Invoke stuff
std::invoke
std::apply
std::make_from_tuple
,std::apply
applied to object constructionis_invocable
,is_invocable_r
,invoke_result
result_of
is_invocable<Foo(Args...), R>
is "can you callFoo
withArgs...
and get something compatible withR
", whereR=void
is default.invoke_result<Foo, Args...>
isstd::result_of_t<Foo(Args...)>
but apparently less confusing?File System TS v1
[class.path]
[class.filesystem.error]
[class.file_status]
[class.directory_entry]
[class.directory_iterator]
and[class.recursive_directory_iterator]
[fs.ops.funcs]
fstream
s can be opened withpath
s, as well as withconst path::value_type*
strings.New algorithms
for_each_n
reduce
transform_reduce
exclusive_scan
inclusive_scan
transform_exclusive_scan
transform_inclusive_scan
Added for threading purposes, exposed even if you aren't using them threaded
Threading
std::shared_mutex
atomic<T>
::is_always_lockfree
scoped_lock<Mutexes...>
std::lock
pain when locking more than one mutex at a time.Parallelism TS v1
std
algorithms, and related machineryhardware_*_interference_size
(parts of) Library Fundamentals TS v1 not covered above or below
[func.searchers]
and[alg.search]
[pmr]
std::function
for allocatorsstd::sample
, sampling from a range?Container Improvements
try_emplace
andinsert_or_assign
Splicing for
map<>
,unordered_map<>
,set<>
, andunordered_set<>
non-const
.data()
for string.non-member
std::size
,std::empty
,std::data
std::begin
/end
Minimal incomplete type support in containers
Contiguous iterator "concept"
constexpr
iteratorsThe
emplace
family of functions now returns a reference to the created object.Smart pointer changes
unique_ptr<T[]>
fixes and otherunique_ptr
tweaks.weak_from_this
and some fixed to shared from thisOther
std
datatype improvements:{}
construction ofstd::tuple
and other improvementsMisc
C++17 library is based on C11 instead of C99
Reserved
std[0-9]+
for future standard librariesdestroy(_at|_n)
,uninitialized_move(_n)
,uninitialized_value_construct(_n)
,uninitialized_default_construct(_n)
std
implementations exposedstd::clamp()
std::clamp( a, b, c ) == std::max( b, std::min( a, c ) )
roughlygcd
andlcm
std::uncaught_exceptions
std::as_const
std::bool_constant
_v
template variablesstd::void_t<T>
std::owner_less<void>
std::less<void>
, but for smart pointers to sort based on contentsstd::chrono
polishstd::conjunction
,std::disjunction
,std::negation
exposedstd::not_fn
std
std::default_order, indirection over(breaks ABI of some compilers due to name mangling, removed.)std::less
.Traits
Deprecated
<codecvt>
memory_order_consume
result_of
, replaced withinvoke_result
shared_ptr::unique
, it isn't very threadsafeIsocpp.org has has an independent list of changes since C++14; it has been partly pillaged.
Naturally TS work continues in parallel, so there are some TS that are not-quite-ripe that will have to wait for the next iteration. The target for the next iteration is C++20 as previously planned, not C++19 as some rumors implied. C++1O has been avoided.
Initial list taken from this reddit post and this reddit post, with links added via googling or from the above isocpp.org page.
Additional entries pillaged from SD-6 feature-test list.
clang's feature list and library feature list are next to be pillaged. This doesn't seem to be reliable, as it is C++1z, not C++17.
these slides had some features missing elsewhere.
While "what was removed" was not asked, here is a short list of a few things ((mostly?) previous deprecated) that are removed in C++17 from C++:
Removed:
register
, keyword reserved for future usebool b; ++b;
<functional>
stuff,random_shuffle
std::function
There were rewordings. I am unsure if these have any impact on code, or if they are just cleanups in the standard:
Papers not yet integrated into above:
P0505R0 (constexpr chrono)
P0418R2 (atomic tweaks)
P0512R0 (template argument deduction tweaks)
P0490R0 (structured binding tweaks)
P0513R0 (changes to
std::hash
)P0502R0 (parallel exceptions)
P0509R1 (updating restrictions on exception handling)
P0012R1 (make exception specifications be part of the type system)
P0510R0 (restrictions on variants)
P0504R0 (tags for optional/variant/any)
P0497R0 (shared ptr tweaks)
P0508R0 (structured bindings node handles)
P0521R0 (shared pointer use count and unique changes?)
Spec changes:
Further reference:
papers grouped by year; not all accepted
https://isocpp.org/files/papers/p0636r0.html