任何人都可以总结一下函数模板重载的想法吗?重要的是,模板参数或功能参数?返回值怎么样?
例如,给定一个功能模板
template<typename X, typename Y> void func(X x, Y y) {}
Run Code Online (Sandbox Code Playgroud)
什么是重载的功能模板?
1) template<typename X> void func(X x, int y) {}
2) template<typename X, typename Y> X func(X x, Y y) {}
3) template<class X, class Y, class Z> void func(X x, Y y, Z z) {}
Run Code Online (Sandbox Code Playgroud) public class Primitive {
void m(Number b, Number ... a) {} // widening, autoboxing->widening->varargs
void m(byte b, Number ... a) {} // unboxing, autoboxing->widening->varargs
public static void main(String[] args) {
Byte b = 12;
Primitive obj = new Primitive();
obj.m(b, 23);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索过并发现扩展优先级高于取消装箱,因此在上面的方法调用中,应该调用第一个方法,因为第二个参数对于两者都是相同的.但这不会发生.你可以解释一下吗?
主要来自 Python 背景,我在使用 C++ 类型时有些挣扎。
我试图通过将不同类型作为参数的几个重载构造函数之一来初始化一个类变量。我已经读过使用auto关键字可以用于变量的自动声明,但是在我的情况下,它不会被初始化,直到选择了构造函数。然而,编译器对不初始化不满意value。
class Token {
public:
auto value;
Token(int ivalue) {
value = ivalue;
}
Token(float fvalue) {
value = fvalue;
}
Token(std::string svalue) {
value = svalue;
}
void printValue() {
std::cout << "The token value is: " << value << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
在 python 中,这可能如下所示:
class Token():
def __init__(self, value):
self.value = value
def printValue(self):
print("The token value is: %s" % self.value)
Run Code Online (Sandbox Code Playgroud)
auto在这种情况下使用关键字的正确方法是什么?我应该完全使用不同的方法吗?
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) 有没有人建议在C#中使用params进行方法参数传递.我正在考虑为前6个参数进行重载,然后使用params功能进行7次重载.我的理由是避免params功能所需的额外数组分配.这适用于一些高性能的实用方法.有什么建议?创建所有重载是浪费代码吗?
以下代码无法编译.
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 ++中声明这样的函数:
int operator + (int , int);
Run Code Online (Sandbox Code Playgroud)
您的回答将不胜感激!
谢谢
假设我有一个与原始指针一起使用的模板:
template<typename T>
void processPointer(T* ptr);
Run Code Online (Sandbox Code Playgroud)
我不希望用void*指针调用它.看来我有两个选择.我可以删除非模板重载:
void processPointer(void*) = delete;
Run Code Online (Sandbox Code Playgroud)
或者我可以删除模板实例化:
template<>
void processPointer<void>(void*) = delete;
Run Code Online (Sandbox Code Playgroud)
声明非模板过载更容易(没有尖角支架).是否有理由我更喜欢删除模板实例化?
我正在尝试const在类中调用函数,但const存在具有相同名称的非函数.
注意:我不能只改变名字.
class MyQuestion
{
void fun()
{
cout<<"a";
}
void fun()const
{
cout<<"b";
}
void call()
{
fun();//<how to call fun() const?
}
};
Run Code Online (Sandbox Code Playgroud) 示例:这是合法的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)