標籤:prim 不能 blog logs public expr log ret other
練習7.53
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 5 using namespace std; 6 7 class Debug{ 8 public: 9 constexpr Debug(bool b = true) : hw(b), io(b), other(b) { }10 constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) { }11 constexpr bool any() { return hw || io || other; }12 void set_io(bool b) { io = b;}13 void set_hw(bool b) { hw = b;}14 void set_other(bool b) { hw = b;}15 private:16 bool hw;17 bool io;18 bool other;19 20 }; 21 22 23 int main()24 {25 constexpr Debug io_sub(false, true, false);26 if(io_sub.any())27 cerr << "print appropriate error messages" << endl;28 constexpr Debug prod(false);29 if(prod.any())30 cerr << "print an error message" << endl;31 32 return 0;33 }
練習7.54
不應該,因為set_需要將需要的資料成員設定成別的值,因此,不能聲明成constexpr
練習7.55
不是,因為string不是一個字面值常量類。
C++primer 7.5.6節練習