代码是:
#include <iostream>
using namespace std;
// compares two objects
template <typename T> void compare(const T&, const T&){
cout<<"T"<<endl;
};
// compares elements in two sequences
template <class U, class V> void compare(U, U, V){
cout<<"UV"<<endl;
};
// plain functions to handle C-style character strings
void compare(const char*, const char*){
cout<<"ordinary"<<endl;
};
int main() {
cout<<"-------------------------char* --------------------------"<< endl;
char* c="a";
char* d="b";
compare(c,d);
cout<<"------------------------- char [2]---------------------------"<< endl;
char e[]= "a";
char f[]="b";
compare(e,f);
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
结果是:
------------------------- char*----------------------- ---
Ť …
请考虑以下代码:
-module(abc).
-export([f/1]).
f(X) when (X==0) or (1/X>2) -> X+100;
f(X) ->X.
Run Code Online (Sandbox Code Playgroud)
和abc:f(0).得到结果0,但为什么1/X不抛异常?
请考虑以下代码.那里发生了什么?
#include <iostream>
#include <stdio.h>
using namespace std;
template<class T>
void test(T &t) // but why int&& is valid here,when instantiated with test<int&>??
{
puts("T&");
}
int tref(int&& param) //compile error
{
puts("tref&");
}
int main()
{
int i;
test<int&>(i);
}
Run Code Online (Sandbox Code Playgroud) 当我从 learnyousomeerlang.com 阅读一篇文章时,我遇到了一个问题。 http://learnyousomeerlang.com/errors-and-processes
它说:
异常来源:
exit(self(), kill)未捕获的结果:
** exception exit: killed被困结果:
** exception exit: killed哎呀,看那个。看来这个人,真的是不可能被困住的。让我们检查一下。
但它不符合我用代码打击测试的内容:
-module(trapexit).
-compile(export_all).
self_kill_exit()->
process_flag(trap_exit,true),
Pid=spawn_link(fun()->timer:sleep(3000),exit(self(),kill) end),
receive
{_A,Pid,_B}->io:format("subinmain:~p output:~p~n",[Pid,{_A,Pid,_B}])
end.
Run Code Online (Sandbox Code Playgroud)
输出为:**4> trapexit:self_kill_exit()。
subinmain:<0.36.0> 输出:{'EXIT',<0.36.0>,killed}**
并且不符合 Trapped Result: ** 异常退出:被杀之前说的。哪个是对的???
请考虑以下代码:
class B;
class A
{
public:
A() {}
A(B &b);
};
class B {};
A::A(B &b) {}
int main()
{
B b;
const A &refa = b;// does this line create a temporary value?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:代码是否const A &refa = b;创建了临时值?
这就是代码,并带有错误:"非法成员初始化:'a'不是基础或成员",错误信息的含义是什么,为什么?
class A{
public:
int a;
};
class B:public A{
public:
B();
};
B::B():a(10){ // put "a(10)" into the constructor body is right
}
Run Code Online (Sandbox Code Playgroud) 为什么以下重载函数调用不明确?编译错误:
调用重载'test(long int)'是模棱两可的,候选者是:void test(A)| 无效测试(B)|
代码:
class A
{
public:
A(int){}
A(){}
};
class B: public A
{
public:
B(long){}
B(){}
};
void test(A a)
{
}
void test(B b)
{
}
void main()
{
test(0L);
return;
}
Run Code Online (Sandbox Code Playgroud) 使用mingw32-g ++编译时,有错误:没有匹配函数来调用'for_each(int [9],int*,main():: Output)',但在vs2005中可以做得好吗?
#include <iostream>
#include <algorithm>
int main() {
struct Output {
void operator () (int v) {
std::cout << v << std::endl;
}
};
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::for_each(a, a + sizeof(a)/sizeof(a[0]), Output());
return 0;
}
Run Code Online (Sandbox Code Playgroud)