Java varargs实现中似乎存在一个错误.当方法使用不同类型的vararg参数重载时,Java无法区分适当的类型.
它给了我一个错误 The method ... is ambiguous for the type ...
请考虑以下代码:
public class Test
{
public static void main(String[] args) throws Throwable
{
doit(new int[]{1, 2}); // <- no problem
doit(new double[]{1.2, 2.2}); // <- no problem
doit(1.2f, 2.2f); // <- no problem
doit(1.2d, 2.2d); // <- no problem
doit(1, 2); // <- The method doit(double[]) is ambiguous for the type Test
}
public static void doit(double... ds)
{
System.out.println("doubles");
}
public static void doit(int... is)
{
System.out.println("ints"); …Run Code Online (Sandbox Code Playgroud) 是否可以拥有相同类的多个版本,这些版本仅在模板参数的数量上有所不同?
例如:
template<typename T>
class Blah {
public:
void operator()(T);
};
template<typename T, typename T2>
class Blah {
public:
void operator()(T, T2);
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试模拟仿函数类型的东西,它可以采用可变数量的参数(最多可写出不同模板的数量).
以下F#代码声明了基类和后代类.基类有一个带有默认实现的虚拟方法'Test'.后代类重写基类方法,并添加新的重载'Test'方法.此代码编译良好,并且在访问后代'Test'方法时不会出现任何问题.
F#代码:
module OverrideTest
[<AbstractClass>]
type Base() =
abstract member Test : int -> int
default this.Test x = x + 1
type Descendant() =
inherit Base()
override this.Test x = x - 1
member this.Test (x, y) = x - y
Run Code Online (Sandbox Code Playgroud)
但是,尝试从C#调用后代的'Test'覆盖会导致编译错误:
var result = td.Test(3); < - 方法'Test'没有重载需要1个参数
完整的C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
var td = new OverrideTest.Descendant();
var result = td.Test(3);
Console.WriteLine(result); …Run Code Online (Sandbox Code Playgroud) 以下代码无法编译.
package varargspkg;
public class Main {
public static void test(int... i) {
for (int t = 0; t < i.length; t++) {
System.out.println(i[t]);
}
System.out.println("int");
}
public static void test(float... f) {
for (int t = 0; t < f.length; t++) {
System.out.println(f[t]);
}
System.out.println("float");
}
public static void main(String[] args) {
test(1, 2); //Compilation error here quoted as follows.
}
}
Run Code Online (Sandbox Code Playgroud)
发出编译时错误.
对于测试的引用是不明确的,varargspkg.Main中的方法test(int ...)和varargspkg中的方法test(float ...)匹配
这似乎是显而易见的,因为在方法调用的参数值test(1, 2);可以提升int以及float
如果任何一个或两个参数后缀为F或f,则编译.
但是,如果我们使用相应的包装器类型表示方法签名中的接收参数,如下所示 …
示例:这是合法的C++ 14吗?
#include <iostream>
static int d() {
return 42;
}
static int e(int d = d()) {
return d;
}
int main() {
std::cout << e() << " " << e(-1) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 5.4 -std=c++14喜欢它,但clang ++ 3.8 -std=c++14抱怨:
samename.cxx:3:23: error: called object type 'int' is not a function or function pointer
static int e(int d = d()) {return d;}
~^
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个子类的TForm
这是我现在的代码:
interface
TfrmEndoscopistSearch = class(TForm)
public
/// original constructor kept for compatibility
constructor Create(AOwner : TComponent); overload; override;
/// additional constructor allows for a caller-defined base data set
constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); overload;
end;
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但我总是得到编译器警告:
[Warning] test.pas(44): Method 'Create' hides virtual method of base type 'TCustomForm'
考虑假设的对象层次结构,从以下开始:
TFruit = class(TObject)
public
constructor Create(Color: TColor); virtual;
end;
Run Code Online (Sandbox Code Playgroud)
及其后代:
TApple = class(TFruit)
public
constructor Create(); overload; virtual;
constructor Create(Color: TColor); overload; override; //deprecated. Calls other constructor - maintaining the virtual constructor chain
end;
Run Code Online (Sandbox Code Playgroud)
这里的想法是我已经覆盖了基类的虚构造函数,其重载也恰好是虚拟的.
德尔福抱怨:
方法'Create'隐藏基类型'TFruit'的虚方法
除了它不隐藏它 - 它就在那里!
这是怎么回事?
While working with ref-qualified function overloads, I'm getting different results from GCC (4.8.1) and Clang (2.9 and trunk). Consider the following code:
#include <iostream>
#include <utility>
struct foo
{
int& bar() &
{
std::cout << "non-const lvalue" << std::endl;
return _bar;
}
//~ int&& bar() &&
//~ {
//~ std::cout << "non-const rvalue" << std::endl;
//~ return std::move(_bar);
//~ }
int const& bar() const &
{
std::cout << "const lvalue" << std::endl;
return _bar;
}
int const&& bar() const …Run Code Online (Sandbox Code Playgroud) 在下面的代码段,在函数调用f(1),1是文字型的int并在第一功能void f(double d)参数类型是double和第二函数void f(short int i)参数类型是短整型.
这1是一个int不是double类型的类型,那么为什么编译器会产生模糊错误?
#include <iostream>
using namespace std;
void f(double d) // First function
{
cout<<d<<endl;
}
void f(short int i) // Second function
{
cout<<i<<endl;
}
int main()
{
f(1); // 1 is a literal of type int
return 0;
}
Run Code Online (Sandbox Code Playgroud) overloading ×10
c++ ×4
delphi ×2
java ×2
ambiguous ×1
c# ×1
c++11 ×1
c++14 ×1
constructor ×1
f# ×1
interop ×1
java-6 ×1
overriding ×1
polymorphism ×1
scope ×1
short ×1
templates ×1