rt
经过 assert
,第 48 行 translate
函数返回 nullopt
。
但没有 assert
时 cout
仍输出了四个空行。
class object{
private: char s[4][4];
public:
object();
...
friend ostream& operator<<(ostream& os,const object& obj);
...
optional<object> translate(pair<int,int> deflect);
...
};
object::object(){
for(int i=0;i<4;++i){
for(int j=0;j<4;++j) s[i][j]='.';
}
}
ostream& operator<<(ostream& os,const object& obj){
for(int i=0;i<4;++i){
for(int j=0;j<4;++j) os<<obj.s[i][j];
os<<endl;
}
return os;
}
optional<object> object::translate(pair<int,int> deflect){
object res;
auto [x,y]=deflect;
auto isvalid=[](int x,int y){return (x>=0&&x<4&&y>=0&&y<4);};
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
int nx=i+x,ny=j+y;
if(!isvalid(nx,ny)) return nullopt;
res.s[nx][ny]=s[i][j];
}
}
return res;
}
object a;
int main(){
ios::sync_with_stdio(0);
cin>>a;
assert(a.translate({1,1})!=nullopt);
cout<<*a.translate({1,1});
return 0;
}
问题:
*nullopt
)是否属于 UB?cout
仍然正常输出了空行?cout
是否输出了不可见字符?