标签: explicit-conversion

扩展方法和显式铸造

我正在使用某些程序集中的类(源代码不可用),因此无法更改其代码我需要为显式转换运算符添加扩展方法,有没有办法实现这一点?(我试图添加为常规扩展方法,但没有成功)

 public static explicit operator MembershipUser(this MembershipUser membership, User user)
    {
        return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user.IsLocked,
            user.CreateDate, user.LastLoginDate, user.LastActivityDate, user.CreateDate, DateTime.MinValue);
    }
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个问题?

.net c# extension-methods casting explicit-conversion

8
推荐指数
2
解决办法
9010
查看次数

isSet()或operator void*()或显式opertor bool()还是其他什么?

什么是最先进的大约功能检查值是否设置与否

例如,下面的迭代器解析单元格.一些单元格包含值,其他单元格为空.

什么是最方便的方式?

struct iterator 
{                                  //usage:
  bool isset() const               // if (it.isset()) 
  bool isSet() const               // if (it.isSet()) 
  bool empty() const               // if (it.empty()) 

  bool is_set()   const            // if (it.is_set()) 
  bool is_valid() const            // if (it.is_valid()) 

  operator void*() const;          // if (it) 

  explicit operator bool() const;  // if ((bool)it) or if(it) //thanks @stijn
  operator          bool() const;  // if (it) //why not implicit conversion?

  bool operator!() const;          // if (!!it)

  //throwing exception as pointed out by …
Run Code Online (Sandbox Code Playgroud)

c++ operator-keyword explicit-conversion c++11 c++03

8
推荐指数
2
解决办法
2599
查看次数

C#显式运算符和对象

请先查看代码.

这是我的自定义类:

public class float2D
{
    public float X { get; private set; }
    public float Y { get; private set; }

    public float2D(float x, float y)
    {
        this.X = x;
        this.Y = y;
    }

    public static explicit operator Point(float2D x)
    {
        return new Point((int)x.X, (int)x.Y);
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

这是我写的测试代码:

    private void TEST()
    {
        float2D V = new float2D(1, 1);
        Point P = Point.Empty;
        P = (Point)V; // Works
        P = (Point)(V as object); // Specified cast is not …
Run Code Online (Sandbox Code Playgroud)

c# typeconverter explicit-conversion

8
推荐指数
2
解决办法
2585
查看次数

"if(getline(fin,str)){}"是否符合C++ 11标准?

我检查了C++ 11标准,发现了以下事实:

  1. std::getline(fin, str)返回一个basic_ios对象,其类具有成员函数explicit operator bool() const;

  2. 该类basic_ios没有成员函数operator void*() const;作为pre-C++ 11.

所以,我认为if (getline(fin, str)) {}不符合标准.它应该写成

if (bool(getline(fin, str)){}.(但是,VC++ 2012会对此用法发出警告.即强制void*为bool)

我对么?

c++ iostream type-conversion explicit-conversion c++11

7
推荐指数
3
解决办法
610
查看次数

为什么我的"显式运算符bool()"没有被调用?

#include <iostream>

using namespace std;

struct A
{
    explicit operator bool() const
    {
        return true;
    }

    operator int()
    {
        return 0;
    }
};

int main()
{
    if (A())
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的期望是A()将在上下文中转换为bool使用my operator bool(),因此打印true.

但是,输出false显示已operator int()被调用.

为什么我explicit operator bool没有按预期召唤?

c++ boolean type-conversion explicit-conversion c++11

7
推荐指数
1
解决办法
1148
查看次数

在C++中,我们可以使用{}进行C风格的转换吗?

虽然我一直在阅读有关数据类型转换的内容,但我看到了这个例子:

void intval()
{
    for (char c; cin >> c; )
    cout << "the value of '" << c << "' is " << int{c} << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我知道我们可以使用:

  1. int(c)
  2. (int) c
  3. static_cast<int>(c)

我的问题:

Q1:是int{c}另一种投射数据类型的方式吗?

Q2:在对网络进行一些研究之后,我知道C++转换是不同的,它有编译器在编译时检查转换的可能性,但是1和2之间有什么区别?int{c}如果它只是另一种铸造方式又有何不同?

问题3:有没有其他方法可以显式转换/转换?

c++ casting type-conversion explicit-conversion

7
推荐指数
2
解决办法
894
查看次数

为什么C++隐式转换有效,但显式转换不起作用?

以下代码在C++ 11中成功编译:

#include "json.hpp"
using json = nlohmann::json ;

using namespace std ;

int main(){
    json js = "asd" ;
    string s1 = js ; // <---- compiles fine
    //string s2 = (string)js ; // <---- does not compile
}
Run Code Online (Sandbox Code Playgroud)

它包括JSON for Modern C++.一个工作示例就在这个wandbox中.

JSON变量js隐式转换为字符串.但是,如果我取消注释最后一行,这是一个显式转换,它就无法编译.编译结果在这里.

除了这个json库的特殊细微差别,你如何编写一个类,以便隐式转换工作,但一个明确的转换不起作用?
是否有某种构造函数限定符允许此行为?

c++ casting implicit-conversion explicit-conversion c++11

7
推荐指数
1
解决办法
761
查看次数

if (static_cast&lt;bool&gt;(x)) 与 if (x)

我有一位同事经常在条件语句中对 bool 进行显式强制转换,如下所示:

\n\n
SomeType *ptr = /* some value */;\nif (static_cast<bool>(ptr)) {\n    // do something\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我一直找不到如此冗长的充分理由。有任何想法吗?

\n\n

编辑:同事说 \xe2\x80\x99 是为了清晰和明确,但我不\xe2\x80\x99t 发现这个解释非常引人注目。我在这里提出这个问题是为了看看其他 C++ 专家是否建议这样做,如果是的话,也许会出现一个更有说服力的论点。

\n

c++ casting conditional-statements implicit-conversion explicit-conversion

7
推荐指数
2
解决办法
1439
查看次数

当枚举包含具有相同值的元素时,如何将原始类型值转换为枚举值?

我写了代码:

enum FlipRotate2dEnum : byte {
    NO = 0,    None               = 0,
    R2 = 1,    RotateTwice        = 1,
    FX = 2,    FlipX              = 2,
    FY = 3,    FlipY              = 3,
    D1 = 4,    ReflectDiagonal1   = 4,
    D2 = 5,    ReflectDiagonal2   = 5,
    RC = 6,    RotateClockwise    = 6,
    RN = 7,    RotateNonClockwise = 7
}
class EnumTest {
    public static void Main() {
        for(byte i = 0; i < 8; ++i) {
            FlipRotate2dEnum v = (FlipRotate2dEnum)i;
            System.Console.WriteLine("{0} {1}", i, v); …
Run Code Online (Sandbox Code Playgroud)

.net c# enums type-conversion explicit-conversion

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

为什么我的显式构造函数会为我的转换运算符造成这种歧义?

我无法弄清楚为什么我的转换运算符正在考虑显式构造函数。

\n
#include <utility>\n\ntemplate <typename T = void>\nstruct First\n{\n    template <typename... Targs>\n    First(Targs&&... args) {}\n};\n\ntemplate <>\nstruct First<void> {};\n\ntemplate <typename T>\nstruct Second\n{\n    template <typename... Targs>\n    Second(Targs&&... args) {}\n};\n\ntemplate <typename... T> class A;\n\ntemplate <typename SecondType>\nclass A<SecondType>\n{\n  public:\n    A(const A&) = default;\n    explicit A(const First<void>& first) {}\n    explicit A(const Second<SecondType>& second) {}\n};\n\ntemplate <typename FirstType, typename SecondType>\nclass A<FirstType, SecondType>\n{\n  public:\n    A(const First<FirstType> & first) {}\n    explicit operator A<SecondType>() const { return A<SecondType>(First<>()); }\n};\n\nint main() {\n    A<int, float> a{First<int>(123)};\n    A<float> b = static_cast<A<float>>(a);\n\n    // test.cpp:41:41: …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor explicit-conversion

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