copy-initialization

Difference between $a=&$b , $a = $b and $a= clone $b in PHP OOP

一曲冷凌霜 提交于 2019-12-23 17:09:26
问题 What is the difference between $a = &$b , $a = $b and $b = clone $a in PHP OOP? $a is an instance of a class. 回答1: // $a is a reference of $b, if $a changes, so does $b. $a = &$b; // assign $b to $a, the most basic assign. $a = $b; // This is for object clone. Assign a copy of object `$b` to `$a`. // Without clone, $a and $b has same object id, which means they are pointing to same object. $a = clone $b; And check more info with References, Object Cloning. 回答2: // $a has same object id as $b.

Is it possible to infer template parameters of tuple from brace-type initialization?

房东的猫 提交于 2019-12-18 17:11:34
问题 In this example, is it possible to allow the deduction of the template parameters type of the tuple ? #include<tuple> #include<string> template<class T1, class T2> void fun(std::tuple<T1, T2> t, std::string other){} int main(){ fun(std::tuple<double, int>(2.,3), std::string("other")); // ok fun(std::make_tuple(2.,3), std::string("other")); // ok, but trying to avoid `make_tuple` fun({2.,3},std::string("other")); // desired syntax but // giving compilation error: candidate template ignored:

Strange behavior of copy-initialization, doesn't call the copy-constructor!

落爺英雄遲暮 提交于 2019-12-18 15:51:39
问题 I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T y = a; //copy-initialization What I understand from reading about copy-initialization is that it needs accessible & non-explicit copy-constructor, or else the program wouldn't compile. I verified it by writing the following code: struct A { int i; A(int i) : i(i) { std::cout << " A(int i)" << std::endl; } private: A(const A &a) { std::cout << " A(const A &)" << std:

Explicit and non-explicit constructors

懵懂的女人 提交于 2019-12-12 04:43:55
问题 class Test { public: Test(int i) { cout<<"constructor called\n";} Test(const Test& t) { cout<<" copy constructor called\n";} }; class Test1 { public: Test1(int i) { cout<<"constructor called\n";} explicit Test1(const Test1& t) { cout<<" copy constructor called\n";} }; int main() { Test t(0); Test u = 0; //Test1 t1(0); Line 1 //Test1 u1 = 0; Line 2 } I observe different outputs. Case 1: When Line 1 and Line 2 are commented the o/p is : constructor called constructor called Case 2: When Line 1

Why is a conversion operator not called when using initialization syntax, and why does the clang error message seem wrong?

安稳与你 提交于 2019-12-10 18:18:57
问题 I have the following code, which constructs one object t2 using a explicit conversion constructor, which performs an implicit conversion of t1. This is expected, and is described in The C++ Programming Language, in section 11.4.1 of the 3rd edition. #include <iostream> #include <string> using namespace std; class test1 { public: test1() {} operator string() { cout << "test1 string conversion operator called" << endl; return string(); } }; class test2 { public: test2() {} test2(string s) {

copy initialization : why move or copy constructor was not called even if copy-elision is turned off?

最后都变了- 提交于 2019-12-10 17:12:15
问题 My question is different because I may "know" copy-elision. I am learning copy initialization. However,the following code confused me because I have already turned off the copy-elision using -fno-elide-contructors -O0 option. #include <iostream> using namespace std; class test{ public : test(int a_, int b_) : a{a_}, b{b_} {} test(const test& other) { cout << "copy constructor" << endl; } test& operator=(const test& other) { cout << "copy assignment" << endl; return *this; } test(test&& other)

Is it possible to infer template parameters of tuple from brace-type initialization?

不想你离开。 提交于 2019-11-30 15:40:52
In this example, is it possible to allow the deduction of the template parameters type of the tuple ? #include<tuple> #include<string> template<class T1, class T2> void fun(std::tuple<T1, T2> t, std::string other){} int main(){ fun(std::tuple<double, int>(2.,3), std::string("other")); // ok fun(std::make_tuple(2.,3), std::string("other")); // ok, but trying to avoid `make_tuple` fun({2.,3},std::string("other")); // desired syntax but // giving compilation error: candidate template ignored: couldn't infer template argument 'T1' void fun(std::tuple<T1, T2> t) } I added the second argument other

This is not copy-initializing, or is it?

本秂侑毒 提交于 2019-11-30 14:04:19
In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8). I try to prove the compilers wrong by making the ctor non explicit and then declaring the copy constructors as deleted. Are the compilers wrong or is there any other explanation? #include <iostream> template <typename T> struct xyz { constexpr xyz (xyz const &) = delete; constexpr xyz (xyz &&) = delete; xyz & operator = (xyz const &) = delete; xyz & operator = (xyz &&) = delete; T i; /*explicit*/ constexpr xyz (T i): i(i) { } };

Strange behavior of copy-initialization, doesn't call the copy-constructor!

可紊 提交于 2019-11-30 13:44:56
I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T y = a; //copy-initialization What I understand from reading about copy-initialization is that it needs accessible & non-explicit copy-constructor , or else the program wouldn't compile. I verified it by writing the following code: struct A { int i; A(int i) : i(i) { std::cout << " A(int i)" << std::endl; } private: A(const A &a) { std::cout << " A(const A &)" << std::endl; } }; int main() { A a = 10; //error - copy-ctor is private! } GCC gives an error ( ideone )

This is not copy-initializing, or is it?

北战南征 提交于 2019-11-29 19:52:39
问题 In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8). I try to prove the compilers wrong by making the ctor non explicit and then declaring the copy constructors as deleted. Are the compilers wrong or is there any other explanation? #include <iostream> template <typename T> struct xyz { constexpr xyz (xyz const &) = delete; constexpr xyz (xyz &&) = delete; xyz & operator = (xyz const &