void f(int){}
typedef void (*f_ptr)(int);
struct Functor{
void operator()(int){}
};
struct X{
operator f_ptr(){ return f; }
};
struct Y{
operator Functor(){ return Functor(); }
};
int main(){
X x; Y y;
x(5); // works ?!
y(5); // doesn't ?!
}
Run Code Online (Sandbox Code Playgroud)
Ideone上的实例.输出:
错误:调用'(Y)(int)'不匹配
Q1:为什么调用x(5)允许,即使X只定义了转换为函数指针,而不是operator()?
Q2:相反,如果我们定义转换为另一个仿函数,为什么不允许相同的事情?
如果我定义:
class Bar(object):
@staticmethod
def bar():
# code
pass
class Foo(Bar):
# code
pass
Run Code Online (Sandbox Code Playgroud)
函数调用Foo.bar()是否可以确定类名Foo?
鉴于函数原型及其在内存中的地址的知识,是否可以从另一个进程或一些只知道原型和内存地址的代码调用此函数?如果可能,如何在代码中处理返回的类型?
我试图在 python 中使用 f 字符串将一些变量替换为我正在打印的字符串,但出现语法错误。这是我的代码:
print(f"{index+1}. {value[-1].replace("[Gmail]/", '')}")
Run Code Online (Sandbox Code Playgroud)
我在添加替换后才开始遇到问题。我已经检查过很多次了,我确信我没有漏掉括号。我知道还有很多其他方法可以实现此目的,其中一些可能更好,但我很好奇为什么这行不通。
我正在尝试为自己的教育编写一个C语法分析器.我知道我可以使用像YACC这样的工具来简化流程,但我想从经验中尽可能多地学习,所以我从头开始.
我的问题是我应该如何处理这样的一行:
doSomethingWith((foo)(bar));
Run Code Online (Sandbox Code Playgroud)
它可能(foo)(bar)是一个类型转换,如:
typedef int foo;
void doSomethingWith(foo aFoo) { ... }
int main() {
float bar = 23.6;
doSomethingWith((foo)(bar));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者,它可能(foo)(bar)是一个函数调用,如:
int foo(int bar) { return bar; }
void doSomethingWith(int anInt) { ... }
int main() {
int bar = 10;
doSomethingWith((foo)(bar));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,解析器无法单独通过查看行来确定它处理的两种情况中的哪一种.doSomethingWith((foo)(bar));这让我很烦恼,因为我希望能够将解析阶段与您实际确定的"解释"阶段分开该行typedef int foo;表示foo现在是有效类型.在我想象的场景中,Type a = b + c * d即使Type,a,b,c和d没有在任何地方定义,也会解析得很好,并且只有在实际尝试"解析"标识符时才会出现问题.
所以,我的问题是:"真正的"C解析器如何处理这个?两个阶段之间的分离是我希望只是一个天真的愿望,还是我错过了什么?
任何人都可以解释为什么A::f(const B& b)和之间存在歧义f(const A::B& b)。我认为代码对意图非常明确。
#include <iostream>
namespace A
{
class B
{
protected:
double value_;
public:
B() : value_(15.0) {}
double getValue() const {return value_;}
};
void f(const B& b)
{
std::cout << "f(b) = " << b.getValue() << std::endl;
}
}
void f(const A::B& b)
{
std::cout << "Other f(b) = " << b.getValue() << std::endl;
}
int main()
{
A::B b;
A::f(b);
f(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,error: call of overloaded ‘f(A::B&)’ is …
我有
index.php
<select id="year_list" name="year_list" onchange="check_year_event('year_list', 'event_list');" > . . . </select>
<select id="event_list" name="event_list" onchange="check_year_event('year_list', 'event_list');" > . . . </select>
.
.
.
<?php
function checkYearandEvent($year, $event) {
$year_event = mysql_query("SELECT * FROM year_event WHERE year = '$event' AND event = '$event'")
if (mysql_num_rows($year_event) > 0) {
// do this
}
}
?>
Run Code Online (Sandbox Code Playgroud)
myscripts.js
function check_year_event(year_id, event_id) {
var year = document.getElementById(year_id).value;
var event = document.getElementById(event_id).value;
// call PHP function (but I don't know how): checkYearandEvent(year, event);
} …Run Code Online (Sandbox Code Playgroud) 我读到当程序进行函数调用时,被调用函数必须知道如何返回其调用者.
我的问题是:被调用函数如何知道如何返回其调用者?是否有通过编译器在幕后工作的机制?
如果要从内联汇编中调用C/C++函数,可以执行以下操作:
void callee() {}
void caller()
{
asm("call *%0" : : "r"(callee));
}
Run Code Online (Sandbox Code Playgroud)
然后GCC将发出如下所示的代码:
movl $callee, %eax
call *%eax
Run Code Online (Sandbox Code Playgroud)
这可能会有问题,因为间接调用会破坏旧CPU上的管道.
由于地址callee最终是常量,可以想象可以使用i约束.引自GCC在线文档:
'我"
允许使用立即整数操作数(具有常量值的操作数).这包括符号常量,其值仅在汇编时或以后知道.
如果我尝试这样使用它:
asm("call %0" : : "i"(callee));
Run Code Online (Sandbox Code Playgroud)
我从汇编程序中收到以下错误:
错误:后缀或操作数对`call'无效
这是因为GCC会发出代码
call $callee
Run Code Online (Sandbox Code Playgroud)
代替
call callee
Run Code Online (Sandbox Code Playgroud)
所以我的问题是是否可以使GCC输出正确call.
以下是一个完美运行的代码示例:
#include<iostream>
#include<vector>
template< class D, template< class D, class A > class C, class A = std::allocator< D > >
void foo( C< D, A > *bar, C< D, A > *bas ) {
std::cout << "Ok!" << std::endl;
}
int main( ) {
std::vector< int > *sample1 = nullptr;
std::vector< int > *sample2 = nullptr;
foo( sample1, sample2 );
return( 0 );
}
Run Code Online (Sandbox Code Playgroud)
但是,在下面的代码中,编译器无法将std :: vector <int>*与nullptr匹配为第二个参数,甚至能够从第一个参数中扣除模板类型.
#include<iostream>
#include<vector>
template< class D, template< class D, class A > …Run Code Online (Sandbox Code Playgroud) function-call ×10
c++ ×5
c ×4
python ×2
abi ×1
c++11 ×1
casting ×1
f-string ×1
functor ×1
gcc ×1
javascript ×1
namespaces ×1
nullptr ×1
parsing ×1
php ×1
quotes ×1
quoting ×1
reflection ×1
stack-frame ×1