我很无聊,想出了这样的黑客(伪代码):
1 struct proxy {
2 operator int(); // int function
3 operator double(); // double function
4 proxy(arguments);
5 arguments &arguments_;
6 };
7
8 proxy function(arguments &args) {
9 return proxy(args);
10 }
11 int v = function(...);
12 double u = function(...);
Run Code Online (Sandbox Code Playgroud)
在真实的代码中使用它是邪恶的吗?
我可能的使用场景是例如数组元素的产品,可能会/可能不会溢出:
int size(short *array);
short size(short *array);
Run Code Online (Sandbox Code Playgroud)
如果使用模板,则可以从函数参数推断函数的原因,而不是模板参数
我想知道你是否可以在python字典中添加一个属性.
class myclass():
def __init__():
self.mydict = {} #initialize a regular dict
self.mydict.newattribute = "A description of what this dictionary will hold"
>>>AttributeError: 'dict' object has no attribute 'newattribute'
setattr(self.mydict,"attribute","A description of what this dictionary will hold"
>>>AttributeError: 'dict' object has no attribute 'newattribute'
Run Code Online (Sandbox Code Playgroud)
无论如何都要快速添加我的描述属性,而不必复制dict类并重载构造函数.我觉得这很简单,但我想我错了.
谢谢,J
考虑下面的函数bar,其参数具有从重载调用初始化的默认值foo:
#include <iostream>
int foo(int x)
{
std::cout << "foo(int)" << std::endl;
return 0;
}
template<typename T>
void bar(T a, int x = foo(T(0))) {}
double foo(double x)
{
std::cout << "foo(double)" << std::endl;
return 0;
}
int main()
{
bar<int>(1);
bar<double>(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个程序输出
foo(int)
foo(double)
Run Code Online (Sandbox Code Playgroud)
对应于foo在bar实例化时可见的两个重载.
相反,当编译时g++-4.6,输出是
$ g++-4.6 -std=c++0x test.cpp; ./a.out
foo(int)
foo(int)
Run Code Online (Sandbox Code Playgroud)
在实现与正常重载分辨率不同的默认参数值时是否考虑了过载集?这种情况是在ISO C++标准中描述的吗?
我认为在重载期间,编译器会检查形式参数是否属于同一类型.例如:
void a(int x)
void a(double x)
Run Code Online (Sandbox Code Playgroud)
因为两个"x"具有差异类型,所以可以过载.
但是,以下两种是否有不同的类型?
void f(int y)
void f(int& y)
Run Code Online (Sandbox Code Playgroud)
我知道一个是PBV和另一个PBR.但第二个y的类型是"int"也是对的吗?为什么编译成功?
PS我注意到虽然它编译,但它不会运行,报告模糊性的运行时错误.
decltype 如果您正在调用它的函数被重载,则会失败,如下面的代码所示:
#include <iostream>
int test(double x, double y);
double test(int x, int y);
char test(char x, int y);
int main()
{
std::cout << decltype(test) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
error: decltype cannot resolve address of overloaded function
Run Code Online (Sandbox Code Playgroud)
我明白这是因为decltype无法弄清楚你试图获得哪种类型的功能.但为什么没有另一种方法来实现这项工作,如下所示:
std::cout << decltype(test(double, double)) << std::endl;
Run Code Online (Sandbox Code Playgroud)
或这个:
double x = 5, y = 2;
std::cout << decltype(test(x, y)) << std::endl;
Run Code Online (Sandbox Code Playgroud)
由于函数不能简单地基于返回类型重载,不会将数据类型或实际变量传递给decltype调用足以告诉它应该检查哪些重载?我在这里错过了什么?
显然,不允许在ref-qualifiers上重载 - 如果删除&或者&&(只是标记,而不是它们的函数),这段代码将无法编译:
#include <iostream>
struct S {
void f() & { std::cout << "Lvalue" << std::endl; }
void f() && { std::cout << "Rvalue" << std::endl; }
};
int main()
{
S s;
s.f(); // prints "Lvalue"
S().f(); // prints "Rvalue"
}
Run Code Online (Sandbox Code Playgroud)
换句话说,如果您有两个具有相同名称和类型的函数,则必须定义两者,如果您定义其中任何一个.我认为这是故意的,但是原因是什么?例如,为什么不允许调用&&rvalues 的版本(如果已定义),以及f()以下变体中的其他所有内容的"primary" (反之亦然 - 尽管这会让人感到困惑):
struct S {
void f() { std::cout << "Lvalue" << std::endl; }
void f() && { std::cout << "Rvalue" …Run Code Online (Sandbox Code Playgroud) 考虑简单的代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (char){}
int main() {
foo(a);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,正如预期的gcc,clang和VC++选择的那样foo(char).
现在让我们稍微修改一下代码:
#include<iostream>
struct A {
operator double(){
std::cout<<"Conversion function double chosen."<<std::endl;
return 1.1;
}
operator char(){
std::cout<<"Conversion function char chosen."<<std::endl;
return 'a';
}
} a;
void foo(int){}
void foo (double){} //parameter changed from char to double
int main() {
foo(a); …Run Code Online (Sandbox Code Playgroud) c++ overloading conversion-operator language-lawyer overload-resolution
C++允许在一个名称空间中使用具有相同名称的类和函数:
struct S {};
void S() {}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,纯名称S意味着功能S.要使用struct,您需要struct在name之前显式添加.
它也可以制作功能模板并仍然使用它们:
template <class T>
void S() {}
struct S {};
Run Code Online (Sandbox Code Playgroud)
但禁止使用模板结构
void S() {}
template <class T>
struct S {};
Run Code Online (Sandbox Code Playgroud)
并给出如下错误:
Run Code Online (Sandbox Code Playgroud)error: redefinition of 'S' as different kind of symbol
这是什么原因?为什么不允许在这里使用模板结构?是否有一个地方使用明确的关键字任何情况struct之前S(如非模板版本)没能解决命名冲突,如果它被允许?也许存在提案?
试试以下代码:
#include <functional>
#include <memory>
class C {
public:
void F(std::function<void(std::shared_ptr<void>)>){}
void F(std::function<void(std::shared_ptr<int>)>){}
};
int main(){
C c;
c.F([](std::shared_ptr<void>) {});
}
Run Code Online (Sandbox Code Playgroud)
您将看到编译错误:
prog.cc:12:7: error: call to member function 'F' is ambiguous
c.F([](std::shared_ptr<void>) {});
~~^
prog.cc:6:10: note: candidate function
void F(std::function<void(std::shared_ptr<void>)>){}
^
prog.cc:7:10: note: candidate function
void F(std::function<void(std::shared_ptr<int>)>){}
^
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这种模棱两可的问题?也许与SFINAE?
overloading ×10
c++ ×9
c++11 ×3
function ×2
attributes ×1
decltype ×1
destructor ×1
dictionary ×1
python ×1
reference ×1
rvalue ×1
shared-ptr ×1
std ×1
syntax ×1
templates ×1