从C++ 11草案,7.5(第1段):
具有不同语言链接的两种函数类型是不同的类型,即使它们在其他方面是相同的.
所以我可以根据语言链接做过载:
extern "C" typedef void (*c_function)();
typedef void (*cpp_function)();
void call_fun(c_function f)
{
}
void call_fun(cpp_function f)
{
}
extern "C" void my_c()
{
}
void my_cpp()
{
}
int main()
{
call_fun(my_c);
call_fun(my_cpp);
}
Run Code Online (Sandbox Code Playgroud)
但是,使用GCC 4.7.1,此示例代码提供了错误消息:
test.cpp: In function 'void call_fun(cpp_function)':
test.cpp:7:6: error: redefinition of 'void call_fun(cpp_function)'
test.cpp:4:6: error: 'void call_fun(c_function)' previously defined here
Run Code Online (Sandbox Code Playgroud)
使用CLang ++:
test.cpp:7:6: error: redefinition of 'call_fun'
void call_fun(cpp_function f)
^
test.cpp:4:6: note: previous definition is here
void call_fun(c_function f)
^
Run Code Online (Sandbox Code Playgroud)
现在的问题是: …
众所周知,模板参数可以是指向成员函数的指针.
所以我可以写:
struct Bar
{
int fun(float x);
};
template <int (Bar::*FUN)(float)>
struct Foo
{ /*...*/ };
typedef Foo<&Bar::fun> FooBar;
Run Code Online (Sandbox Code Playgroud)
但是如果我希望Bar类型本身是模板参数呢?
template <typename B, int (B::*FUN)(float)>
struct Foo
{ /*...*/ };
typedef Foo<Bar, &Bar::fun> FooBar;
Run Code Online (Sandbox Code Playgroud)
现在,当我使用它时,我必须写Bar两次!
我的问题是:有没有办法强制编译器自动推导出类类型?
目标是让这个工作:
typedef Foo<&Bar::fun> FooBar;
typedef Foo<&Moo::fun> FooMoo;
Run Code Online (Sandbox Code Playgroud) 从借来的指针教程(破碎),有点修改:
struct Point {x: float, y: float}
fn compute(p1 : &Point) {}
fn main() {
let shared_box : @Point = @Point {x: 5.0, y: 1.0};
compute(shared_box);
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,因为共享框是自动借用的功能.
但做一个特点:
struct Point {x: float, y: float}
trait TPoint {}
impl TPoint for Point {}
fn compute(p1 : &TPoint) {}
fn main() {
let shared_box : @TPoint = @Point {x: 5.0, y: 1.0} as @TPoint;
compute(shared_box);
// ^~~~~~~ The error is here
}
Run Code Online (Sandbox Code Playgroud)
它失败了,(编译器版本0.6)说:
错误:不匹配的类型:预期
&TPoint但找到@TPoint(特征存储不同:预期但发现@)
这是编译器中的错误吗?或者是不允许使用特征的借用指针? …
以下代码无法编译(playground):
#![allow(unused)]
use std::cell::RefCell;
enum Token {
A,
B,
}
struct Thing {
c: Token,
}
fn main() {
let a = RefCell::new(Thing { c: Token::A });
if let Token::A = a.borrow().c {
//Error!
}
}
Run Code Online (Sandbox Code Playgroud)
它失败并出现此错误:
error[E0597]: `a` does not live long enough
--> src/main.rs:16:23
|
16 | if let Token::A = a.borrow().c {
| ^ borrowed value does not live long enough
...
19 | }
| - `a` dropped here while still borrowed
|
= …Run Code Online (Sandbox Code Playgroud) [:alpha:]如果我正在制作需要它的unicode正则表达式,那相当于什么.
例如,[:word:]它是[\w]
如果我得到一些帮助,将会很棒.
我有一个简单的问题.我有以下代码:
class Class1
{
Class1();
~Class1();
void func1();
private:
char* c;
}
void Class1::func1()
{
string s = "something";
this->c = s.c_str();
}
Run Code Online (Sandbox Code Playgroud)
将c存储"something"的时候func1()完成?
我对C++中的默认构造函数有疑问.例如,在A类中,使用此默认构造函数A(){};或有A() = default;什么区别?它们之间的一般区别是什么?
先感谢您!
谁能告诉ma关于终端递归调用与非终端递归调用之间的分歧?
我写
#include <iostream>
int factorial(int n)
{
if (n==0 || n==1)
return 1;
else
return n*factorial(n-1);
}
Run Code Online (Sandbox Code Playgroud)