Implicit conversion not allowed on return

#include <optional>

bool f() {
  std::optional<int> opt;
  return opt;
}

Does not compile: 'return': cannot convert from 'std::optional<int>' to 'bool'

Consulting reference I would have thought to find an explanation, but I read it as it should be ok.

Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2; in particular:

  • when the expression is used as the argument when calling a function that is declared with T2 as parameter;
  • when the expression is used as an operand with an operator that expects T2;
  • when initializing a new object of type T2, including return statement in a function returning T2;
  • when the expression is used in a switch statement (T2 is integral type);
  • when the expression is used in an if statement or a loop (T2 is bool).

std::optional doesn't have any facility for implicitly converting to bool. (Allowing implicit conversions to bool is generally considered a bad idea, since bool is an integral type so something like int i = opt would compile and do completely the wrong thing.)

std::optional does have a "contextual conversion" to bool, the definition of which looks similar to a cast operator: explicit operator bool(). This cannot be used for implicit conversions; it only applies in certain specific situations where the expected "context" is a boolean one, like the condition of an if-statement.

What you want is opt.has_value().

Implicit conversions, Implicit conversions are performed whenever an expression of some type T1 is E is only allowed if E has a single non-explicit user-defined conversion function to that E has non-explicit conversion functions whose return types are (possibly specified type T . Note that explicit conversion functions are not considered,� Implicit conversions are attempted when: An argument supplied to a function does not have the same type as the matching parameter. The value returned from a function does not have the same type as the function return type. An initializer expression does not have the same type as the object it is initializing.


Implicit conversion in SQL Server, When we look at the following sample, SQL Server does not convert will return the same result set and we will avoid the implicit conversion� These include xml, bigint, and sql_variant. There is no implicit conversion on assignment from the sql_variant data type, but there is implicit conversion to sql_variant. While the above chart illustrates all the explicit and implicit conversions that are allowed in SQL Server, it does not indicate the resulting data type of the conversion.


That's because implicit coversion of std::optional to bool is not supported: https://en.cppreference.com/w/cpp/utility/optional/operator_bool

constexpr explicit operator bool() const noexcept;

You have to explicitly convert to bool as bool(opt) or simply use opt.has_value() instead.

SQL SERVER, Let us learn how to fix error 217 for Implicit conversion. Implicit conversion from data type datetime to int is not allowed. SQL SERVER – Maintain Carriage Return (Enter Key) in SQL Server 2016 Management Studio While� Although the return type is not allowed in the declaration of a user-defined conversion function, the decl-specifier-seq of the declaration grammar may be present and may include any specifier other than type-specifier or the keyword static, In particular, besides explicit, the specifiers inline, virtual, constexpr (since C++11), consteval (since C++20), and friend are also allowed (note that friend requires a qualified name: friend A::operator B();).


This isn't really about implicit conversion, this is about the type of initialization.

What optional has is an explicit conversion function, i.e.

explicit operator bool() const; 

From N4849 [class.conv.fct]/p2

A conversion function may be explicit (9.2.2), in which case it is only considered as a user-defined conversion for direct-initialization.

The above means that these cases will use the conversion function : [dcl.init]/p16

The initialization that occurs (16.1) — for an initializer that is a parenthesized expression-list or a braced-init-list, (16.2) — for a new-initializer (7.6.2.7), (16.3) — in a static_cast expression (7.6.1.8), (16.4) — in a functional notation type conversion (7.6.1.3), and (16.5) — in the braced-init-list form of a condition is called direct-initialization.

However, these cases will not use the conversion function : [dcl.init]/p15

The initialization that occurs in the = form of a brace-or-equal-initializer or condition (8.5), as well as in argument passing, function return, throwing an exception (14.2), handling an exception (14.4), and member initialization (9.4.1), is called copy-initialization.

The example in the question falls under the copy initialization case and does not use optional's conversion function.

Data type conversion (Database Engine), When data from a Transact-SQL result column, return code, or output Some implicit and explicit data type conversions are not supported� User-defined implicit conversions should behave in that way as well. If a custom conversion can throw an exception or lose information, define it as an explicit conversion. User-defined conversions are not considered by the is and as operators. Use a cast expression to invoke a user-defined explicit conversion. Use the operator and implicit or


More on Implicit Conversions - Microsoft Tech Community, Implicit conversion from data type datetime to int is not allowed. Returning to the three implicit conversion options above, SQL Server never� So as to summarize, the implicit keyword should be used to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data. However, to prevent unexpected results, care must be taken care dealing with implicit conversions because it does not require programmers to explicitly


Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query. Therefore, is there any method to return datetime value from stored procedure?


Implicit conversions with classes In the world of classes, implicit conversions can be controlled by means of three member functions: Single-argument constructors: allow implicit conversion from a particular type to initialize an object. Assignment operator: allow implicit conversion from a particular type on assignments.