Can "T t = {};" and "T t{};" produce different results?
If you consider a case in which one statement will compile, but the other will not compile as "different effects," then yes, here's a context:
#include <iostream>
class T {
public:
int data{ 0 };
explicit T() {
data = 0;
std::cout << "Default constructor" << std::endl;
}
};
int main()
{
T t1 = {};
T t2{};
return 0;
}
The line declaring/initializing t1
gives the following, with clang-cl
:
error : chosen constructor is explicit in copy-initialization
The MSVC
compiler also complains:
error C2512: 'T': no appropriate default constructor available
message : Constructor for class 'T' is declared 'explicit'
The difference is in explicit
. I've managed to make msvc difference, but it looks like a compiler bug:
#include <iostream>
#include <initializer_list>
struct T
{
template<class... A>
T(A...) {std::cout << "1\n";}
explicit T() { std::cout << "2\n"; }
};
int main()
{
T t1 = {}; // 1
T t2{}; // 2
}