Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CV限定类型不能转换为CV非限定类型_C++ - Fatal编程技术网

C++ CV限定类型不能转换为CV非限定类型

C++ CV限定类型不能转换为CV非限定类型,c++,C++,我已经写了一个例子: #include <iostream> struct A { A(const A&){ std::cout << "A(const A&)" << std::endl; } A(){ std::cout << "A()" << std::endl; } }; struct B { B(){ std::cout << "B()" << std::en

我已经写了一个例子:

#include <iostream>

struct A
{
    A(const A&){ std::cout << "A(const A&)" << std::endl; }
    A(){ std::cout << "A()" << std::endl; }
};

struct B
{
    B(){ std::cout << "B()" << std::endl; }
    operator A(){ std::cout << "operator A()" << std::endl; return A(); }
};

B b;

void foo(A) { }

int main(){ std::cout << "main function starting..." << std::endl; foo(b); }
尽管本标准在第13.3.3.1/6节[over.best.ics]中规定:

顶级简历资格的任何差异均包含在 初始化本身并不构成转换


我希望有人能给我解释一下为什么上一个例子不起作用。

你引用的那一行在标题为13.3.3.1.1标准转换序列的章节下。从B到A的转换是用户定义的转换。用户定义转换规则在本标准下一节中,标题为13.3.3.1.2用户定义转换顺序


您引用的内容不适用于用户定义的转换。

在第二个示例中,运算符A{肯定需要是运算符A常量{?
#include <iostream>

struct A
{
    A(const A&){ std::cout << "A(const A&)" << std::endl; }
    A(){ std::cout << "A()" << std::endl; }
};

struct B
{
    B(){ std::cout << "B()" << std::endl; }
    operator A(){ std::cout << "operator A()" << std::endl; return A(); }
};

const B b;

void foo(A) { }

int main(){ std::cout << "main function starting..." << std::endl; foo(b); }