标签: copy-initialization

是否可以从大括号类型初始化推断元组的模板参数?

在这个例子中,是否可以允许推导 的模板参数类型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)
}
Run Code Online (Sandbox Code Playgroud)

我添加了第二个参数,other以避免涉及函数级别的可变参数的解决方案fun。另外,我试图避免使用make_tuple,至少在用户代码中(即在main())。事实上,它不需要是tuple所涉及的类型,只要允许“所需的语法”,并且可以在稍后阶段以某种方式推断出其元素类型。

(另外,虽然相似,但这无关,initializer_list因为它在大括号中具有不同的元素根本不起作用)

它至少在clang 3.2和时失败gcc 4.7.2。它是否有希望与当前或不久的将来的标准兼容?(例如未来(?)initializer_tuple。)

(这对于通过聚合子元素来增加函数调用的表现力非常有用,但这可以争论)


注意:对于示例代码,似乎std::forward_as_tuple …

c++ tuples explicit copy-initialization c++11

6
推荐指数
1
解决办法
946
查看次数

复制初始化 - 从'int'转换为非标量类型

我想知道如何定义类my_int,以便int中的转换std::complex< my_int >由编译器完成,而不是由我手动完成.

如果4未转换为" my_int",则以下程序无法编译

// Example program
#include <iostream>
#include <string>
#include <complex>

struct my_int
{
    my_int() : _i(0) {}
    my_int(const my_int& mi) : _i(mi._i) {}

    my_int(int i) : _i(i) {}
    operator int(){return _i;}

    int _i;
};

std::ostream& operator<<(std::ostream& os, const my_int& mi)
{
    os << mi._i;
    return os;
}

int main()
{
  std::complex<my_int> ci = 4; // Casting 4 to my_int works

  std::cout << ci;
}
Run Code Online (Sandbox Code Playgroud)

我知道,如果你初始化cistd::complex<my_int> ci(4)它的工作原理,但我想它与拷贝初始化工作.

c++ templates copy-initialization c++11 c++14

6
推荐指数
1
解决办法
391
查看次数

C++ 中不可复制的类可以通过值捕获吗?

在下一个程序中,B删除了复制构造函数的结构被抛出并按值捕获:

struct B {
    B() = default;
    B(const B&) = delete;
};

int main() {
    try {
        throw B{};
    }
    catch( B ) {
    }
}
Run Code Online (Sandbox Code Playgroud)

Clang 拒绝该代码并出现预期错误:

error: call to deleted constructor of 'B'
    catch( B ) {
Run Code Online (Sandbox Code Playgroud)

然而GCC很好地接受了该程序,演示: https: //gcc.godbolt.org/z/ed45YKKo5

这里是哪个编译器?

c++ initialization exception language-lawyer copy-initialization

6
推荐指数
1
解决办法
126
查看次数

模板类中没有匹配的函数

当我尝试在我的mingw32编译器上编译此代码时,我没有得到匹配的成员函数错误

#include <iostream> 
using std::cout;
template <class T>
class Pattern
{
public:
    Pattern(): element(){

        cout<< "default c-tor";
        }

    Pattern(Pattern &copy): element(copy.element){

        cout<< "copy c-tor";

        }

    Pattern& operator=(Pattern &assgn)
    {
        cout<<" assignment operator";
       element = assgn.element;
       return *this;
    }

    ~Pattern(){

        cout<<"destructor";

        }

private:
     T element;
};

template <class T>
Pattern<T> creator()
{
   cout<< "Testing creator";
   Pattern<T> pat;
   return pat;
}

int main()
{
   Pattern<double> pt1(creator<double>());
   Pattern<double> pt2 = creator<double>();
}
Run Code Online (Sandbox Code Playgroud)

有人请告诉我如何解决问题.

c++ templates member-functions copy-initialization

5
推荐指数
0
解决办法
230
查看次数

为什么我不能从字符串中复制初始化字符串流?

以下代码在GCC,Clang和Visual Studio中失败:

#include <string>
#include <sstream>

int main() {
    std::string s = "hello"; // ok, copy-initialization
    std::stringstream ss1(s); // ok, direct-initialization
    std::stringstream ss2 = s; // error
}
Run Code Online (Sandbox Code Playgroud)

我认为直接初始化的唯一情况是复制初始化不起作用的时候构造函数是显式的,在这种情况下不是这样.这是怎么回事?

c++ string stringstream copy-initialization

4
推荐指数
1
解决办法
1907
查看次数

为什么在使用初始化语法时没有调用转换运算符,为什么 clang 错误消息似乎是错误的?

我有以下代码,它使用显式转换构造函数构造一个对象 t2,该构造函数执行 t1 的隐式转换。这是预期的,并在 C++ 编程语言的第 3 版第 11.4.1 节中进行了描述。

#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) {
        cout << "test2 string conversion constructor called" << endl;
    }
};

int main() {
    test1 t1;
    test2 t2(t1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你所期望的:

> clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on …
Run Code Online (Sandbox Code Playgroud)

c++ clang implicit-conversion copy-initialization c++11

4
推荐指数
1
解决办法
4025
查看次数

复制初始化:为什么即使关闭copy-elision也没有调用移动或复制构造函数?

我的问题不同,因为我可能"知道"复制省略.我正在学习复制初始化.但是,以下代码使我感到困惑,因为我已经使用-fno-elide-contructors -O0选项关闭了copy-elision .

#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)
    {
        cout << "move constructor" << endl;
    }
    test& operator=(test&& other)
    {
        cout <<"move assignment" << endl;
        return *this;
    }

private :
    int a;
    int b;
};

test show_elide_constructors()
{
    return …
Run Code Online (Sandbox Code Playgroud)

c++ initialization implicit-conversion copy-initialization copy-elision

4
推荐指数
1
解决办法
139
查看次数

为什么智能指针类型的成员变量不能在类的声明处初始化?

当我想向类添加智能指针类型的成员变量时,我发现它无法在声明处初始化:

class Foo {
 public:
  std::shared_ptr<int> intSharedPtr = new int;  // not ok
  Foo() {}
};
Run Code Online (Sandbox Code Playgroud)

但我可以这样做:

class Foo {
 public:
  std::shared_ptr<int> intSharedPtr;  // ok
  int* intPtr = new int; // ok
  Foo() {
    intSharedPtr.reset(new int);
  }
};
Run Code Online (Sandbox Code Playgroud)

看来智能指针与普通指针有很大不同,为什么会出现这种情况呢?

c++ initialization class smart-pointers copy-initialization

4
推荐指数
1
解决办法
218
查看次数

PHP OOP中$ a =&$ b,$ a = $ b和$ a = clone $ b之间的差异

是什么区别$a = &$b,$a = $b并且$b = clone $a在PHP OOP?$a是一个类的实例.

php clone reference class copy-initialization

3
推荐指数
1
解决办法
2165
查看次数

为什么使用字符串字面量的class_copy初始化初始化不适用于string_view?

我有以下代码:

#include <string_view>

class Foo
{
public:
    Foo(std::string_view) {}
};
Run Code Online (Sandbox Code Playgroud)

当我这样做时,一切都可以正常编译(使用clang v8,C ++ 17):

Foo f("testing");
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用副本初始化,它将失败:

Foo f = "testing";
Run Code Online (Sandbox Code Playgroud)

诊断:

prog.cc:15:9: error: no viable conversion from 'const char [8]' to 'Foo'
    Foo f = "testing";
        ^   ~~~~~~~~~
prog.cc:7:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [8]' to 'const Foo &' for 1st argument
class Foo
      ^
prog.cc:7:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from …
Run Code Online (Sandbox Code Playgroud)

c++ initialization copy-initialization

3
推荐指数
1
解决办法
73
查看次数