标签: conversion-operator

为什么switch和if语句与转换运算符的行为不同?

为什么switchif语句在转换运算符方面表现不同?

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 integerif声明中它被完美地识别为a bool.(GCC)

c++ if-statement switch-statement conversion-operator explicit-conversion

15
推荐指数
2
解决办法
2101
查看次数

重载转换函数模板

考虑以下:

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

c++ conversion-operator language-lawyer overload-resolution

14
推荐指数
1
解决办法
114
查看次数

具有多个功能和多个转换运算符的过载分辨率

考虑简单的代码:

#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

13
推荐指数
2
解决办法
348
查看次数

哪些转换应该含糊不清?

我有一些代码如下:

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

13
推荐指数
1
解决办法
232
查看次数

在C++中键入转换/转换混淆

什么是类型转换?什么是类型转换

我什么时候应该使用它们?

细节:对不起,如果这是一个明显的问题; 我是C++的新手,来自红宝石背景,习惯于to_s等等to_i.

c++ casting type-conversion conversion-operator

12
推荐指数
1
解决办法
6320
查看次数

是`使用Base :: operator T`允许`T`是模板类型参数?

考虑这个例子:

struct B { operator int(); };

template<class T>
struct X:B
{
    using B::operator T;
};
Run Code Online (Sandbox Code Playgroud)

GCC接受代码,而Clang和MSVC拒绝它.哪个是对的?

请注意,如果基类型是相关的,则所有编译器都接受以下代码:

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)

c++ templates using-declaration conversion-operator

12
推荐指数
1
解决办法
203
查看次数

Clang和GCC对转换运算符直接初始化的合法性存在分歧

最新版本的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

12
推荐指数
1
解决办法
199
查看次数

C++隐式转换运算符优先级

编辑:继迈克·西摩的评论,我换成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)

c++ casting operator-precedence conversion-operator

10
推荐指数
1
解决办法
4263
查看次数

继承模板化转换运算符

考虑以下代码:

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

10
推荐指数
1
解决办法
98
查看次数

Ambiguous string :: operator =调用类型,隐式转换为int和string

鉴于以下计划:

#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.这是有问题的,因为它要含蓄从转换GenericTypeint …

c++ string conversion-operator overload-resolution implicit-conversion

9
推荐指数
1
解决办法
2351
查看次数