也许您是一位C++老手了,不妨来看看,也许有点学院派的味道
一、这样能编译成功吗?假如能的话,能得到正确有答案吗?
是一个菲波拉契数.
f(n)=f(n-1)+f(n-2);
template<int n>
struct Fib { static int Value; };
template <>
int Fib<0>::Value = 0;
template <>
int Fib<1>::Value = 1;
template<int n>
int Fib<n>::Value = Fib<n-1>::Value + Fib<n-2>::Value;
int f ()
{
return Fib<40>::Value;
}
int f3()
{
return Fib<3>::Value;
}
二、这样能编译通过吗?假如能的话,最后一个函数里的x到底是什么东东?
struct A { int i; struct i {}; };
struct B { int i; struct i {}; };
struct D : public A, public B { using A::i; void f (); };
void D::f () { struct i x; }
三、这是常见的了,哪些是有问题的?
i = v[i++];
i = 7, i++, i++;
i = ++i + 1;
i = i + 1;
四、下面这个哪一个行不能编译
struct S {
typedef struct A {} A;
typedef struct B B;
typedef A A;
};
五、下面的代码有问题吗?能得到正确答案吗?
struct A {
template< typename T > operator T() const;
} a;
template<> A::operator float() const
{
return 1.0f;
}
int main()
{
float f = 1.0f * a;
}
六、下面这段代码在你的编译器上能编译成功吗?
struct S {
static int const I = 42;
};
template<int N> struct X {};
template<typename T> void f(X<T::I>*) {}
template<typename T> void f(X<T::J>*) {}
int main() {
f<S>(0);
}
七、这段代码有问题吗?
template<int i> class A { /* ... */ };
template<int i> void g(A<i+1>);
template<int i> void f(A<i>, A<i+1>);
void k() {
A<1> a1;
A<2> a2;
g(a1);
g<0>(a1);
f(a1, a2);
}