在C#中你可以这样做:
foo = string.Format("{0} {1} {2} {3} ...", "aa", "bb", "cc" ...);
Run Code Online (Sandbox Code Playgroud)
此方法Format()接受无限参数,第一个是如何格式化字符串,其余是要放在字符串中的值.
今天我遇到了一种情况,我必须得到一组字符串并测试它们,然后我记得这种语言功能,但我不知道.在几次不成功的网络搜索之后,我意识到获得一个阵列会更加谨慎,这并不能让我非常满意.
问:如何创建一个接受无限参数的函数?我该如何使用它?
我有以下课程:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i)
{
return registers[i];
}
};
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我已经实现了方括号运算符"get".
现在我想实现它的设置,即:risc[1] = 2.
怎么做到呢?
我注意到一本教科书,您可以为标准库函数提供自己的实现,例如swap(x,y)通过函数重载的模板特化.这对于可以从赋值交换以外的其他东西中受益的任何类型都是有用的,STL containers例如(我已经知道了已经写过的掉期).
我的问题是:
什么更好:模板专业化为您的专业交换实现,或函数重载提供您希望在没有模板的情况下使用的确切参数?
为什么更好?或者如果他们是平等的,为什么呢?
c++ stl overloading standard-library template-specialization
我写了这两个重载:
int func(int, int) {
return 1;
}
int func(double, double) {
return 2;
}
Run Code Online (Sandbox Code Playgroud)
当我使用明显的两个调用方案(即func(1, 1)and )调用它们时,分别调用func(1.0, 1.0)了第一个和第二个重载函数,并且当我尝试调用func(1, 1.0)它时会出现错误,但是当我将1a强制转换为 a 时long long,我不会得到一个错误,第二个重载就是被调用的那个。
#include <iostream>
int main()
{
std::cout << func(1, 1); // outputs 1.
std::cout << func(1.0, 1.0); // outputs 2.
// std::cout << func(1, 1.0); // erroneous.
std::cout << func((long long)1, 1.0); // outputs 2.
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?起初,我认为这是因为一些提升,但我尝试了带有两个浮点数的第三次重载,但我无法通过调用它来调用它像func((int)1, 1.0f). 我不知道为什么会不一样,也不知道为什么在long long传递a 时会调用第二个重载。
下面的代码片段在编译过程中会产生一个"foo的foo调用"错误,我想知道如果没有完全限定对foo的调用,是否有任何方法解决这个问题:
#include <iostream>
struct Base1{
void foo(int){
}
};
struct Base2{
void foo(float){
}
};
struct Derived : public Base1, public Base2{
};
int main(){
Derived d;
d.foo(5);
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以,问题就像标题所说的那样.想法?我的意思是,以下工作完美无瑕:
#include <iostream>
struct Base{
void foo(int){
}
};
struct Derived : public Base{
void foo(float){
}
};
int main(){
Derived d;
d.foo(5);
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 好的,所以方法重载是一个糟糕的事情.既然已经解决了这个问题,我们假设我实际上想要重载这样的方法:
static void run(Consumer<Integer> consumer) {
System.out.println("consumer");
}
static void run(Function<Integer, Integer> function) {
System.out.println("function");
}
Run Code Online (Sandbox Code Playgroud)
在Java 7中,我可以使用非模糊的匿名类作为参数轻松地调用它们:
run(new Consumer<Integer>() {
public void accept(Integer integer) {}
});
run(new Function<Integer, Integer>() {
public Integer apply(Integer o) { return 1; }
});
Run Code Online (Sandbox Code Playgroud)
现在在Java 8中,我当然想用lambda表达式调用这些方法,我可以!
// Consumer
run((Integer i) -> {});
// Function
run((Integer i) -> 1);
Run Code Online (Sandbox Code Playgroud)
既然编译器应该能够推断出来Integer,为什么我不离开Integer呢?
// Consumer
run(i -> {});
// Function
run(i -> 1);
Run Code Online (Sandbox Code Playgroud)
但这不编译.编译器(javac,jdk1.8.0_05)不喜欢这样:
Test.java:63: error: reference to run is …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何定义一个适用于多种类型参数的函数(例如int和int64).据我了解,F#中无法实现函数重载(当然编译器会抱怨).以下面的功能为例.
let sqrt_int = function
| n:int -> int (sqrt (float n))
| n:int64 -> int64 (sqrt (float n))
Run Code Online (Sandbox Code Playgroud)
编译器当然抱怨语法无效(似乎不支持模式匹配中的类型约束),尽管我认为这说明了我想要实现的内容:一个对多个参数类型进行操作并返回相应值的函数类型.我觉得在F#中使用泛型类型/类型推断/模式匹配的某种组合是可能的,但语法已经躲过了我.我也尝试过使用:?操作者(动态型测试)和当在模式匹配块子句,但这仍然会产生各种错误.
由于我对这门语言不熟悉,我很可能会尝试在这里做一些不可能的事情,所以如果有其他解决方案,请告诉我.
考虑以下代码:
#include <iostream>
#include <string>
// void f(const char *) { std::cout << "const char *"; } // <-- comment on purpose
void f(const std::string &) { std::cout << "const std::string &"; }
void f(const void *) { std::cout << "const void *"; }
int main()
{
f("hello");
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我编译了这个程序g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026:
$ g++ -std=c++11 strings_1.cpp -Wall
$ ./a.out
const void *
Run Code Online (Sandbox Code Playgroud)
请注意,注释是为了测试而存在,否则编译器使用f(const char *).
那么,为什么编译器会f(const void*)接管f(const std::string …
c++ overloading stdstring string-literals overload-resolution
我有一个 C 库(带有 C 头文件),它存在于两个不同的版本中。
其中之一具有如下所示的函数:
int test(char * a, char * b, char * c, bool d, int e);
Run Code Online (Sandbox Code Playgroud)
另一个版本看起来像这样:
int test(char * a, char * b, char * c, bool d)
Run Code Online (Sandbox Code Playgroud)
(为此, e 不是作为函数参数给出的,而是在函数本身中硬编码的)。
库或其头文件没有定义/包含任何检查库版本的方法,所以我不能只使用#ifor#ifdef来检查版本号。
有什么办法可以编写一个可以用这个库的两个版本编译的C程序,这取决于编译程序时安装的是哪个版本?这样,想要编译我的程序的贡献者可以自由使用库的任一版本,并且该工具可以使用任一版本进行编译。
所以,澄清一下,我正在寻找这样的(或类似的):
#if HAS_ARGUMENT_COUNT(test, 5)
test("a", "b", "c", true, 20);
#elif HAS_ARGUMENT_COUNT(test, 4)
test("a", "b", "c", true);
#else
#error "wrong argument count"
#endif
Run Code Online (Sandbox Code Playgroud)
有没有办法在C中做到这一点?我想不出办法。
该库将是 libogc ( https://github.com/devkitPro/libogc ),它if_config在不久前改变了它的定义,我想让我的程序同时使用旧版本和新版本。我在库中找不到任何版本标识符。目前我使用的是 GCC 8.3 的修改版本。
如何声明一些具有相同名称但在一个类中具有不同数量的参数或不同类型的方法?
我必须在这堂课中改变一下:
class MyClass:
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
def my_method(self,parameter_A_that_Must_Be_String):
print parameter_A_that_Must_Be_String
def my_method(self,parameter_A_that_Must_Be_String,parameter_B_that_Must_Be_String):
print parameter_A_that_Must_Be_String
print parameter_B_that_Must_Be_String
def my_method(self,parameter_A_that_Must_Be_String,parameter_A_that_Must_Be_Int):
print parameter_A_that_Must_Be_String * parameter_A_that_Must_Be_Int
Run Code Online (Sandbox Code Playgroud)