相关疑难解决方法(0)

为什么C++不能从赋值中推断出模板类型?

int x = fromString("test"):无法推断'ValueType'的模板参数

int x = fromString<int>("test") :按预期正常工作

那么为什么编译器在这里挣扎呢?我用各种真实的模板函数看到它,而不仅仅是这个愚蠢的例子.它必须是语言的一个特征,但是什么?

c++ templates stl

16
推荐指数
2
解决办法
6279
查看次数

C语言中的返回类型多态性

为什么我们看不到允许返回类型中具有多态性的callables的类C语言?我可以看到附加类型推断是如何成为障碍的,但是我们有很多语言都有完整的类型推理系统(适用于不同级别的"工作").

编辑:返回类型多态性我的意思是仅在返回类型中重载函数签名.例如,C++和Java只允许在形式参数的类型中重载,而不是在返回类型中.

polymorphism return-type

11
推荐指数
1
解决办法
6031
查看次数

为什么非模板化函数具有相同的名称和参数但返回类型不同是非法的?(但模板功能合法吗?)

我查看了一些相关的堆栈溢出线程,例如模板函数重载的这种情况,这让我无法理解

函数重载按返回类型?

但似乎都没有给我我正在寻找的答案,至少不是一种容易让我解释的方式.

我的问题归结为:从设计和技术的角度来看,为什么这样做是合法的:

#include <iostream>

using namespace std;

template<class T>
void func(){
    cout << "Compiler inferred from void return value!\n";
}

template<class T>
int func(){
    cout << "Compiler inferred from int return value!\n";
    return 0;
}

int main(){
    void (*thisFunc)()=func<int>;
    int (*thatFunc)()=func<int>;
    thisFunc();
    thatFunc();
}
Run Code Online (Sandbox Code Playgroud)

但不是这个:

#include <iostream>

using namespace std;

void func(){
    cout << "You won't see this because it won't compile!\n";
}

int func(){
    cout << "Nor this one!\n";
    return 0;
}

int main(){
    void …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading ambiguity

11
推荐指数
1
解决办法
329
查看次数

C#重载返回类型 - 推荐方法

我有一种情况,我只希望返回类型与方法重载不同,但你不能在C#中这样做.

处理这个问题的最佳方法是什么?事实是我需要这个意味着我的程序结构存在缺陷吗?

鉴于在C#中这是不可能的,推荐的方法是什么?

.net c# overloading return-type

10
推荐指数
2
解决办法
8919
查看次数

为什么没有为不同的返回类型定义方法重载?

在Scala中,您可以通过使用共享公共名称但具有不同arities或不同参数类型的方法来重载方法.我想知道为什么这也没有扩展到方法的返回类型?请考虑以下代码:

class C {
  def m: Int = 42
  def m: String = "forty two"
}

val c = new C
val i: Int = C.m
val s: String = C.m
Run Code Online (Sandbox Code Playgroud)

有什么理由不适用吗?

谢谢,

文森特.

oop methods scala overloading

9
推荐指数
3
解决办法
2901
查看次数

为什么函数不能被返回类型重载?

可能重复:
返回类型的函数重载?
拼图:根据返回值重载C++函数

因为我有一个库,它以下列形式公开了一堆函数:

bool GetVal();
double GetVal();
int GetVal();
long GetVal();
//So on.
Run Code Online (Sandbox Code Playgroud)

现在我必须包装这些.我宁愿不再重写同一组函数.我想做点什么

template<class T>
T GetVal(){}
Run Code Online (Sandbox Code Playgroud)

但我似乎无法让这个工作.有任何想法吗?

c++ types overloading return

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

为什么C#不考虑多态中函数的返回类型?

有两个函数具有相同的名称和相同的参数集,但具有不同的返回类型.为什么它不是多态的形式,即方法重载?为什么编译器不允许这样做?

c# oop polymorphism overloading

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

Is there a way to overload a function based on different Result type in Delphi?

Function overloading by return type?

has a very detailed answer on the rational on function overloading by return type, and from what I can see Delphi does not allow this, but are there any workarounds to overload a function based on different return type in Delphi?

delphi overloading function

6
推荐指数
2
解决办法
1701
查看次数

C#编译器如何处理重载显式转换运算符?

编译器应该翻译这段代码:

public static explicit operator Int64(MyNumber n)
{
    return n.ToInteger();
}

public static explicit operator Double(MyNumber n)
{
    return n.ToDouble();
}
Run Code Online (Sandbox Code Playgroud)

两个方法具有相同的名称和签名,但只有它们的返回类型不同,例如

public static Int64 ExplicitCast(MyNumber n)
...

public static Double ExplicitCast(MyNumber n)
...
Run Code Online (Sandbox Code Playgroud)

但是,我们不允许只有返回类型的方法.窗帘后面会发生什么?

c# compiler-construction

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

C++多个接口只有不同的返回类型?

做一个以人类可读的方式将.NET IL翻译成C++的实验.

这是问题所在: C#允许您使用相同的方法名称来解析多个接口,这些接口的返回类型不同.C++似乎不支持这个,但是使用vTable解决两个接口是不可能的(或者我错了?).

我找到了一种使用模板在C++中复制C#方法的方法,但我想知道是否有一种方法不需要模板来解决同样的问题?模板很冗长,如果可能的话,我不希望它们用于每种接口类型.

这是C++版本.

template<typename T>
class IMyInterface
{
    public: short (T::*Foo_IMyInterface)() = 0;
};

template<typename T>
class IMyInterface2
{
    public: int (T::*Foo_IMyInterface2)() = 0;
};

class MyClass : public IMyInterface<MyClass>, public IMyInterface2<MyClass>
{
    public: MyClass()
    {
        Foo_IMyInterface = &MyClass::Foo;
        Foo_IMyInterface2 = &MyClass::IMyInterface2_Foo;
    }

    public: virtual short Foo()
    {
        return 1;
    }

    private: int IMyInterface2_Foo()
    {
        return 1;
    }
};

class MyClass2 : public MyClass
{
    public: virtual short Foo() override
    {
        return 2;
    }
}; …
Run Code Online (Sandbox Code Playgroud)

c# c++ templates overloading interface

5
推荐指数
2
解决办法
272
查看次数