为什么switch和if语句在转换运算符方面表现不同?
struct WrapperA
{
explicit operator bool() { return false; }
};
struct WrapperB
{
explicit operator int() { return 0; }
};
int main()
{
WrapperA wrapper_a;
if (wrapper_a) { /** this line compiles **/ }
WrapperB wrapper_b;
switch (wrapper_b) { /** this line does NOT compile **/ }
}
Run Code Online (Sandbox Code Playgroud)
编译错误是switch quantity is not an integer在if声明中它被完美地识别为a bool.(GCC)
c++ if-statement switch-statement conversion-operator explicit-conversion
考虑以下:
struct X {
template <class T> operator T(); // #1
template <class T> operator T&(); // #2
};
int a = X{}; // error: ambiguous
int& b = X{}; // calls #2
int const& c = X{}; // calls #2
Run Code Online (Sandbox Code Playgroud)
这种情况b很简单,#2是唯一可行的候选人.表示初始化的#2首选规则是什么,但两者对于初始化是不明确的?#1int const&int
考虑简单的代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (char){}
int main() {
foo(a);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,正如预期的gcc,clang和VC++选择的那样foo(char).
现在让我们稍微修改一下代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (double){} //parameter changed from char to double
int main() {
foo(a); …Run Code Online (Sandbox Code Playgroud) c++ overloading conversion-operator language-lawyer overload-resolution
我有一些代码如下:
class bar;
class foo
{
public:
operator bar() const;
};
class bar
{
public:
bar(const foo& foo);
};
void baz() {
foo f;
bar b = f; // [1]
const foo f2;
bar b2 = f2; // [2]
}
Run Code Online (Sandbox Code Playgroud)
GCC在[2]处给出错误,但不是[1].Clang给出了两个错误,显然MSVC都没有给出错误.谁是对的?
c++ conversion-operator language-lawyer overload-resolution copy-initialization
什么是类型转换?什么是类型转换?
我什么时候应该使用它们?
细节:对不起,如果这是一个明显的问题; 我是C++的新手,来自红宝石背景,习惯于to_s等等to_i.
考虑这个例子:
struct B { operator int(); };
template<class T>
struct X:B
{
using B::operator T;
};
Run Code Online (Sandbox Code Playgroud)
请注意,如果基类型是相关的,则所有编译器都接受以下代码:
template<class T>
struct B { operator T(); };
template<class T>
struct X:B<T>
{
using B<T>::operator T;
};
Run Code Online (Sandbox Code Playgroud) 最新版本的clang(3.9)在第二行拒绝了这段代码f; 最新版本的gcc(6.2)接受它:
struct Y {
Y();
Y(const Y&);
Y(Y&&);
};
struct X {
operator const Y();
};
void f() {
X x;
Y y(x);
}
Run Code Online (Sandbox Code Playgroud)
如果进行了任何这些更改,clang将接受以下代码:
Y移动构造函数const从转换运算符中删除Y y(x)为Y y = x原始示例合法吗?哪个编译器错了?在检查标准中有关转换函数和重载分辨率的部分之后,我还未能找到明确的答案.
c++ initialization type-conversion conversion-operator language-lawyer
编辑:继迈克·西摩的评论,我换成operator std::string () const;用operator char * () const;,并相应地改变了实现.这允许隐式转换,但是,由于某种原因,unsigned long int运算符优先于char*运算符,这只是感觉不对...而且,我不想暴露讨厌的C之类的东西,比如char*class,当我有std :: string时.我有一种预感,我的CustomizedInt类需要继承一些东西才能支持我想要的功能.有人可以详细说明迈克的评论std::basic_string吗?我不确定我是否理解得当.
我有这段代码:
#include <string>
#include <sstream>
#include <iostream>
class CustomizedInt
{
private:
int data;
public:
CustomizedInt() : data(123)
{
}
operator unsigned long int () const;
operator std::string () const;
};
CustomizedInt::operator unsigned long int () const
{
std::cout << "Called operator unsigned long int; ";
unsigned long int output;
output = (unsigned long int)data;
return output;
}
CustomizedInt::operator std::string () const
{ …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
template <class R, class... Args>
using function_type = R(*)(Args...);
struct base {
template <class R, class... Args>
constexpr operator function_type<R, Args...>() const noexcept {
return nullptr;
}
};
struct derived: private base {
template <class R, class... Args>
using base::operator function_type<R, Args...>; // ERROR
};
Run Code Online (Sandbox Code Playgroud)
C++20 中是否有替代方法来继承和公开模板化转换函数?
c++ inheritance conversion-operator template-meta-programming c++20
鉴于以下计划:
#include <iostream>
#include <string>
using namespace std;
struct GenericType{
operator string(){
return "Hello World";
}
operator int(){
return 111;
}
operator double(){
return 123.4;
}
};
int main(){
int i = GenericType();
string s = GenericType();
double d = GenericType();
cout << i << s << d << endl;
i = GenericType();
s = GenericType(); //This is the troublesome line
d = GenericType();
cout << i << s << d << endl;
}
Run Code Online (Sandbox Code Playgroud)
它在Visual Studio 11上编译,但不是clang或gcc.这是有问题的,因为它要含蓄从转换GenericType到int …
c++ string conversion-operator overload-resolution implicit-conversion
c++ ×10
casting ×2
c++20 ×1
if-statement ×1
inheritance ×1
overloading ×1
string ×1
templates ×1