Answer the question
In order to leave comments, you need to log in
What are the brackets in the std::true_type{} expression?
What is this {} in the expression
bool trueValue = true_type{};
And why does the following code print Hello, World correctly?
#include <iostream>
class hello {
public:
operator std::string() {
return "Hello, World!";
}
};
int main(int argc, char** argv)
{
std::string helloWorld = hello{};
std::cout << helloWorld;
return 0;
}
Answer the question
In order to leave comments, you need to log in
C++11 universal initializer. It can contain anything: constructor parameters, initializer_list, structure fields... Your code is equivalent to the code
std::string helloWorld(std::move(hello().operator std::string()));
As far as I understand, in your example it is quite possible to replace curly braces with round ones and nothing will change.
But if the hello class had a constructor that takes a std::initializer_list, then if curly braces were used, this constructor would be called, and not the default constructor, even if the curly braces are empty.
Make both constructors and check which one is called with curly braces and with parentheses.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question