注册 登录
编程论坛 C++教室

我想输出 cout

lixang 发布于 2007-09-28 21:45, 568 次点击
我想输出 cout<<"hh"<<endl;如何做呢?
不要改变我的函数体!

template<class T>
struct Moban
{
Moban() {};
Moban(T &x): val(x) {}
T val;
template<bool cmd>
static void f (int x) { cout<<"hh"<<endl; }
template<>
static void f<true> (int x) { cout<<x<<"true"<<endl;}
template<>
static void f<false> (int x) {cout<<x<<"false"<<endl; }
};
int main()
{
Moban<int>::f<true>(10);
}
4 回复
#2
coachard2007-09-28 22:18
本想看完一本pure C++ programing,C++应该入门了。。。

看到这个,我才知道,我连C++的门把手我还没摸到。。。
#3
Arcticanimal2007-09-29 14:07

永远不可能调用到 static void f(int x) { cout<<"hh"<<endl; }
这就好像
bool cmd;
switch( cmd )
{
case false :
// ...
case true :
// ...
default:
// ...
}
default 部分的语句永远不会被执行

#4
lixang2007-09-29 15:02
正由于如此:才让大家想个办法!看究竟能否解决!
#5
野比2007-09-29 19:25

限制true和false的表示。
1为true,0为false,其他为第三态

1