标签: implicit-conversion

如何仅将特定行为添加到特定类型的列表中?

我有一个小的Scala/Neo4j应用程序,通过"技术"和"interestIn"关系链接人和主题.它有一个REST/Json Api(使用Scalatra),当我想在List [Person]和List [Topic]中添加一个"asJson"方法时,我遇到了一个典型的类型擦除问题.我想为不同的内容类型实现不同的Json序列化行为,但当然这些类型会被删除.到目前为止,我能够提出的最好的是以下运行时技巧:

implicit def topicsOrPeopleAsJson[T](list: List[T]) = new {
    def asJson: String = {
        list match {
            case head :: tail if (head.isInstanceOf[Topic]) => topicsToJson(list.asInstanceOf[List[Topic]])
            case head :: tail if (head.isInstanceOf[Person]) => peopleToJson(list.asInstanceOf[List[Person]])
            case _ => "[]"
        }
    }

    private def peopleToJson(people: List[Person]) = {
        ...
    }

    private def topicsToJson(topics: List[Topic]) = {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但我想知道是否有更好的解决方案,也许包括类型类,一个我不太熟悉的主题(还).

scala neo4j implicit-conversion

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

通过初始标准转换序列区分用户定义的转换序列

该标准似乎提供了两个规则来区分涉及用户定义的转换运算符的隐式转换序列:

13.3.3最佳可行功能[over.match.best]

[...]一个可行的功能F1被定义为比另一个可行的功能F2更好的功能,如果[...]

  • 上下文是用户定义转换的初始化(见8.5,13.3.1.5和13.3.1.6),从返回类型F1到目标类型的标准转换序列(即,被初始化的实体的类型)是比从返回类型F2到目标类型的标准转换序列更好的转换序列.

13.3.3.2对隐式转换序列进行排序[over.ics.rank]

3 - 相同形式的两个隐式转换序列是无法区分的转换序列,除非以下规则之一适用:[...]

  • 用户定义的转换序列U1是比另一个用户定义的转换序列U2更好的转换序列,如果它们包含相同的用户定义的转换函数或构造函数或聚合初始化,并且U1的第二标准转换序列优于第二标准转换序列U2.

据我了解,13.3.3允许编译器区分不同的用户定义的转换运算符,而13.3.3.2允许编译器区分不同的函数(某些函数的重载f),每个函数都需要在参数中进行用户定义的转换(请参阅我的侧边栏给出以下代码(在GCC 4.3中),为什么转换为引用调用两次?).

是否有其他规则可以区分用户定义的转换序列?/sf/answers/96883111/上的答案表明,13.3.3.2:3可以根据隐式对象参数(转换运算符)的cv限定来区分用户定义的转换序列,或者构造函数或聚合初始化的单个非默认参数,但我不知道这是如何相关的,因为这需要比较各个用户定义的转换序列的第一个标准转换序列,标准没有似乎提到了.

假设S1优于S2,其中S1是U1的第一个标准转换序列,S2是U2的第一个标准转换序列,那么U1是否优于U2?换句话说,这段代码是否格式良好?

struct A {
    operator int();
    operator char() const;
} a;
void foo(double);
int main() {
    foo(a);
}
Run Code Online (Sandbox Code Playgroud)

g ++(4.5.1),Clang(3.0)和Comeau(4.3.10.1)接受它,更喜欢非const限定A::operator int(),但我希望它被拒绝为含糊不清且因此格式错误.这是标准中的缺陷还是我对它的理解?

c++ constructor conversion-operator language-lawyer implicit-conversion

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

C++中的嵌套隐式转换

我希望在两个级别进行隐式转换.以下代码片段是我面临的问题的原型.

//Sources
class A
{
public:
    void print()
    {
        std::cout <<"Class A"<< std::endl;
    }

    operator int()
    {
        return 1;
    }
};

class B
{
public:
    void print()
    {
        std::cout <<"Class B"<< std::endl;
    }

    operator A()
    {
        return A();
    }
};

class C
{
public:
    void print()
    {
        std::cout <<"Class C"<< std::endl;
    }
    operator B()
    {
        return B();
    }
};


void print_(A a)
{
    a.print();
}

//driver

int main( int argc, char* argv[] )
{
    C c;

    //print_( c ); // …
Run Code Online (Sandbox Code Playgroud)

c++ conversion-operator implicit-conversion

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

解决复合赋值中的转换警告

在我的代码中,我有很多variable <<= 1;句子,variable其类型为uint16_t.编译器吐出警告说

从'int'转换为'uint16_t'可能会改变其值[-Wconversion]

我该如何解决?我可以使用类似的长形符号variable = (uint16_t)(variable << 1)- 但我想保留简短的符号.

c warnings type-conversion implicit-conversion

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

没有将Symbol隐式转换为Integer

我使用rails 4.0.1和ruby 2.0.0 with devise 3.2.2.当用户尝试编辑他的帐户时,我不断收到此问题.我在application_controller.rb中有我的强参数和一个自定义更新控制器,如下所示:

class RegistrationsController <Devise :: RegistrationsController

def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.for(:account_update)
    @user = User.find(current_user.id)
    @user.balanced_associate_token_with_customer(account_update_params)
    if @user.update_with_password(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
end 
Run Code Online (Sandbox Code Playgroud)

protected def after_update_path_for(resource)user_path(resource)end end

从昨天开始我一直在拉我的头发......任何帮助都会受到赞赏.如果您需要更多信息,请告诉我.

编辑:在我注意到错误之前唯一改变的是设计开始在设计初始化文件中请求config.secret_key.

完整跟踪的一部分显示:

devise (3.2.2) lib/devise/models/database_authenticatable.rb:64:in `[]'
devise (3.2.2) lib/devise/models/database_authenticatable.rb:64:in `update_with_password'
app/controllers/registrations_controller.rb:12:in `update'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
Run Code Online (Sandbox Code Playgroud)

谢谢!

implicit-conversion devise ruby-on-rails-4

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

c中的unsigned int数据类型

请帮我区分C中的这些代码:

代码1:

#include<stdio.h>
#include <stdint.h>

uint8_t fb(int a)
{
    return -3;
}

int main()
{
    int a = fb(-3);
    printf("%d",a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码2:

#include<stdio.h>

unsigned int fb(int a)
{
    return -3;
}

int main()
{
    int a = fb(-3);
    printf("%d",a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是第一个代码按预期返回253但第二个代码返回-3,这是意外的,因为返回类型是无符号的.请帮帮我这是怎么回事?

我用过mingw gcc编译器.

c types implicit-conversion

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

Scala:如何找出StringOps.map使用哪个CanBuildFrom实现

我正在通过"Scala for the Impatient"一书中的练习来学习Scala.一个问题是:

结果"abc".map(_.toUpper)是a String,但结果 "abc".map(_.toInt)是a Vector.找出为什么.

在阅读" Scala集合的架构 "之后,我认为这是因为CanBuildFrom选择了不同的隐式实现.如何找出为上述表达式选择的?

string scala mapreduce builder implicit-conversion

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

Char to int隐式演员幕后故事

以下内容在c#中有效,Char可以隐式转换为int

int i = 'a';
Run Code Online (Sandbox Code Playgroud)

我只是对.Net Framework在幕后做什么很好奇,我查看char并输入int源代码但无法找到它的编写位置.

任何机构都可以解释幕后发生的事情吗?

c# operators implicit-conversion

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

为什么编译器在某些情况下只能隐式地将char*转换为std :: string

这些工作:

struct WithString {
  WithString(std::string){};
};

void takeString(std::string){}

//implicit conversions:
takeString("hello");
WithString("hello");
Run Code Online (Sandbox Code Playgroud)

但这不是:

WithString makeWithString() { return "hello";}

// error: no viable conversion from returned value of type 'const char [6]'...
Run Code Online (Sandbox Code Playgroud)

如果std::string在前两种情况下隐式转换为"hello" ,为什么不能在最后一种情况下呢?请注意,我没有将WithString构造函数指定为explicit,所以我希望这样的转换.

通过这样做,我可以让行为起作用:

struct WithString {
  WithString(std::string){};
  WithString(const char *){};
};
Run Code Online (Sandbox Code Playgroud)

我只是对这种奇怪感到好奇.如果我假定一个猜测,我会说这是因为前两个工作的情况下,转换之间const char *到std::string,但在错误的情况下,这反而会需要2转换链,首先从const char *到std::string,然后从std::string到WithString.也许这就是原因,但我不确定.

c++ stdstring object-construction implicit-conversion char-pointer

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

为什么要使用int而不是bool的转换?

我为一个类实现了两次转换。一种是布尔,一种是int&。

如果我隐式转换为int,则使用int&转换,但是如果我想要布尔型,则仍使用int&转换。

struct A
{
    operator bool() const;
    operator int&();
};

// trying to call these:
A a;
int  i_test = a; // calls operator int&()
bool b_test = a; // calls operator int&()
Run Code Online (Sandbox Code Playgroud)

我知道a会隐式转换为int&然后转换为bool,但是为什么要花更长的时间呢?我如何不用写就可以避免它a.operator bool()?

c++ overload-resolution implicit-conversion

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