Consider the following code example
#include <iostream>
#include <experimental/optional>
std::experimental::optional<int> dflt(42);
template<const std::experimental::optional<int>& Dflt>
void foo() {
if (Dflt) {
std::cout << "default is set" << std::endl;
} else {
std::cout << "default is not set" << std::endl;
}
}
int main() {
foo<dflt>();
foo<std::experimental::nullopt>();
}
I am trying to pass a nulloptnon-type function as a template parameter, but it does not compile. However, it works with a global variable dfltwith static storage.
The compiler error message is as follows:
foo.cc: In function βint main()β:
foo.cc:13:34: error: no matching function for call to βfoo()β
foo<std::experimental::nullopt>();
^
foo.cc:7:6: note: candidate: template<const std::experimental::fundamentals_v1::optional<int>& Dflt> void foo()
void foo() {
^
foo.cc:7:6: note: template argument deduction/substitution failed:
foo.cc:13:34: error: could not convert template argument βstd::experimental::fundamentals_v1::nulloptβ to βconst std::experimental::fundamentals_v1::optional<int>&β
foo<std::experimental::nullopt>();
I know that the example is stupid, but my main question is: can it nulloptbe passed as a non-piggy template parameter?
source
share