The following code compiles successfully with both clang++ 3.8.0 and g++ 7.2.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors
):
struct Foo
{
constexpr operator bool() const
{
return false;
}
};
int main()
{
constexpr bool b = Foo{};
(void)b;
}
Is such behavior of the compilers standard compliant? Note that adding any member (like int i;
) to the Foo
class doesn't change anything.
defaulted default constructor cannot be constexpr because the corresponding implicitly declared default constructor would not be constexpr.
A constexpr function must accept and return only literal types. A constexpr function can be recursive. It can't be virtual. A constructor can't be defined as constexpr when the enclosing class has any virtual base classes.
Java compiler automatically creates a default constructor (Constructor with no arguments) in case no constructor is present in the java class. Following are the motive behind a default constructor. Initialize all the instance variables of the class object.
1) The default constructor is a no-arg constructor with a no-arg call to super(). 2) Instance methods and variables are only accessible after the super constructor runs. both are true. default constructor = no-parameter constructor.
Yes, the implicit constructor is constexpr this case. In general, it depends on the sub-objects.
[class.ctor]
A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (6.2) to create an object of its class type (4.5) or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (15.6.2) and an empty compound- statement. If that user-written default constructor would be ill-formed, the program is ill-formed. If that user-written default constructor would satisfy the requirements of a constexpr constructor (10.1.5), the implicitly-defined default constructor is constexpr. ... [snip]
Relevant requirements of a constexpr constructor:
[dcl.constexpr]
- the class shall not have any virtual base classes;
- for a non-delegating constructor, every constructor selected to initialize non-static data members and base class subobjects shall be a constexpr constructor;
Yes it is. The default constructor that a compiler generates, and the trivial constructor
Foo() = default;
both enable you to write constexpr bool b = Foo{};
, assuming all class members can be constructed constexpr
. Note that if you had written
Foo(){}
then constexpr
would not be allowed. (An important difference between default
and a constructor provided with an empty body.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With