In C++, why does the perfect-forwarding factory call make_logger<Logger>({"core", 3}, flush_hook) fail to compile when a direct Logger constructor call with the same arguments works?
answer
- deduction needs a type
- braces have no type
- an overload set has no single type
- name the type at the call site
basics
~20 sA forwarding reference must deduce a type from each argument. A braced list has no type, and an overloaded function name like flush_hook names several functions, so deduction fails. The direct call works because the constructor's parameter types are already known.
solid answer
~40 sPerfect forwarding works only for arguments whose type can be deduced and bound by a reference. In a direct call, `Logger({"core", 3}, flush_hook)` initializes the `Config` parameter from the braces and picks the `flush_hook` overload matching `void (*)(int)`. Through `make_logger(Args&&... args)` there is no target type yet: a braced-init-list is a non-deduced context, an overload set has no single type, and Clang reports "deduced incomplete pack <(no value), (no value)> for template parameter 'Args'". Other classic failures: a **bit-field**, which no non-const reference can bind; and **`0` or `NULL`** meant as a null pointer, which deduce an integer type that no longer converts to a pointer. Fixes name the type at the call site: `Config{"core", 3}`, `static_cast<void (*)(int)>(flush_hook)`, `nullptr`, or a copy of the bit-field.
code
cpp · 25 lines#include <memory>
#include <string>
#include <utility>
struct Config { std::string name; int level; };
struct Logger {
Logger(Config c, void (*hook)(int)) : cfg(std::move(c)), on_flush(hook) {}
Config cfg;
void (*on_flush)(int);
};
void flush_hook(int) {}
void flush_hook(double) {}
template <class T, class... Args>
std::unique_ptr<T> make_logger(Args&&... args) {
return std::make_unique<T>(std::forward<Args>(args)...);
}
int main() {
Logger direct({"core", 3}, flush_hook); // types known: fine
// make_logger<Logger>({"core", 3}, flush_hook); // does not compile
auto forwarded = make_logger<Logger>(Config{"core", 3},
static_cast<void (*)(int)>(flush_hook));
return forwarded->cfg.level == direct.cfg.level ? 0 : 1;
}go deeper
Know that wrappers like emplace_back cannot take a bare braced list; pass the constructor arguments themselves or name the type.
Explain that forwarding deduces types from arguments first, so anything with no single type - braces, overload sets - cannot pass through.
Recognise each failure from its diagnostic, fix it at the call site, and know the bit-field, null pointer and pre-C++20 aggregate cases.
Weigh whether a team's generic factories are worth these call-site surprises, or whether explicit constructors serve readers better.
## Why forwarding needs a type A direct call and a forwarding call do the same work in a different order: - **Direct call.** `Logger({"core", 3}, flush_hook)` already knows `Logger`'s constructor parameter types, `Config` and `void (*)(int)`. The braces initialize a `Config`, and overload resolution of the address of `flush_hook` picks the overload whose type matches the target. - **Forwarding call.** `make_logger<Logger>({"core", 3}, flush_hook)` must first deduce `Args` from the arguments alone, before any constructor is considered. Anything without a single, deducible type stops it there. Interviewers usually probe a short, well-known list of these failure cases; each one below was compiled with Clang 21. ## The usual failures | Argument | What goes wrong | Fix at the call site | |---|---|---| | braced list `{"core", 3}` | a braced-init-list parameter is a non-deduced context | `Config{"core", 3}` | | overloaded name `flush_hook` | an overload set has no single type | `static_cast<void (*)(int)>(flush_hook)` or a lambda | | function template name | same: no type until arguments are known | name the specialization, e.g. `hook<int>` | | bit-field `h.level` | a non-const reference cannot bind to a bit-field | `+h.level` or copy it to a local | | `0` or `NULL` meant as null | deduces an integer type; a forwarded `int` is not a null pointer constant | `nullptr` | ## What the compiler says The diagnostics come from deep inside the factory or the library, which is why these cases are worth recognising: 1. The braced list and the overloaded name both end in "candidate template ignored: substitution failure [with T = Logger]: deduced incomplete pack <(no value), (no value)> for template parameter 'Args'". 2. The bit-field fails at the call: "non-const reference cannot bind to bit-field 'level'". 3. A forwarded `0` reaches the constructor as an `int` rvalue, and the error points inside `make_unique`: "no matching constructor for initialization of 'Sink'". ## Containers have the same edges `emplace_back` is a forwarding function, so it inherits every failure above: - `v.push_back({1, 2})` on a `std::vector<std::pair<int, int>>` compiles, because `push_back`'s parameter type is the concrete `const value_type&` or `value_type&&` and the braces initialize it. - `v.emplace_back({1, 2})` does not: Clang reports "deduced incomplete pack <(no value)> for template parameter '_Args'". Write `v.emplace_back(1, 2)` instead. - Aggregates are a version-dependent edge: `emplace_back(1, 2)` for `struct P { int x; int y; };` fails in C++17, because forwarding calls `P(1, 2)` with parentheses and aggregates had no such constructor. C++20 added parenthesized aggregate initialization, and the same call compiles. ## Why the wrapper cannot fix it A factory cannot recover information the call never gave it. Workarounds inside the wrapper - an extra `std::initializer_list<U>` overload, a parameter of a fixed type - each fix one case and change overload resolution for every other caller. The honest fix is at the call site: give each argument a type. ## Failures that surface later Not every forwarding problem stops at deduction: - **A `static const` data member with an in-class initializer but no definition.** A direct call taking `int` by value reads the constant without needing storage. Forwarding binds a reference to it, which ODR-uses the member, so a definition is required; without one the program typically fails to link, with the linker reporting the member as an undefined symbol (seen with Clang 21 at `-O0`; optimization can hide it). Declaring it `static constexpr` fixes it, because since C++17 a `constexpr` static data member is implicitly inline and therefore defined. - **String literals.** `"core.log"` forwards as `const char (&)[9]`, an array reference, not as `const char*`. Usually the target converts it without trouble, but a target that overloads on array references or on templates may choose differently than a direct call would. - **Explicit constructors.** Forwarding ends in direct-initialization, so `emplace_back(10)` on a `std::vector<std::vector<int>>` happily calls the `explicit` size constructor and appends a vector of ten zeros, where `push_back(10)` would not compile. Each of these compiles or almost compiles, which is why they are senior-level knowledge: the diagnosis starts from a wrong result or a link error, not from a template error.
- Why does emplace_back(1, 2) on a std::vector of a plain struct with two int members fail in C++17 but compile in C++20?Forwarding ends in direct-initialization with parentheses, `P(std::forward<Args>(args)...)`. In C++17 an aggregate has no constructor taking two `int`s, so that initialization fails. C++20 added parenthesized aggregate initialization, letting `P(1, 2)` initialize the members in order, so `emplace_back(1, 2)` and `std::make_unique<P>(1, 2)` compile.
- How can a factory accept a braced list without the caller naming the type?Only by adding an overload whose parameter is a concrete type, for example `template<class T, class U> std::unique_ptr<T> make_logger(std::initializer_list<U> il)`, because `std::initializer_list<U>` is deducible from a braced list of `U`s. It handles one shape only and adds an overload every caller now competes with, so naming the type at the call site is usually cleaner.
- Perfect forwarding accepts any argument a direct call accepts.
- A braced list is deduced as std::initializer_list for Args&&.
- Forwarding 0 works for a pointer parameter because 0 is a null pointer constant.
- The overloaded function name is resolved after the target constructor is chosen.
- Bit-fields forward as references to their containing object.
Assumes C++23; parenthesized aggregate initialization, which lets emplace_back and make_unique build an aggregate from forwarded arguments, arrived in C++20.