为什么将initializer_list传递给allocotr :: construct构造向量不起作用?

时间:2019-11-06 21:12:22

标签: c++ allocator

我在这里遇到问题:我再次在阅读C ++入门5版。现在我在 { case 1: getnames(); break; case 2: getNumber(); break; case 3: cout << "End of Program.\n"; break; default: cout << "Not a Valid Choice. \n"; cout << "Choose again.\n"; break; } 了,一切都很好,但是我不知道为什么我的代码没有普及:

stage("1") {
    def localSuccess = catchLocalError {
        // do stuff
    }
    if(!localSuccess) {
        // do something special if we're unstable
    }
}

boolean catchLocalError(Closure c) {
    try {
        c()
        return true
    } catch (Exception e) {
        return false
    }
}
  • 为什么调用传递整数的std::allocator不能编译-因为我猜#include <iostream> #include <initializer_list> #include <memory> #include <vector> void print_vec(vector<int> v) { for (const auto i : v) std::cout << i << ", "; std::cout << std::endl; } int main(int argc, char* argv[]) { //print_vec(5); // fails. vector(size_t) is explicit std::allocator<vector<int>> a; auto p = a.allocate(10); a.construct(p, 5);// works?! //a.construct(p + 1, {7, 16, 23, 81, 77, 24, 10}); // doesn't work even vector(initilizer_list) is not explicit a.construct(p + 1, vector<int>({7, 16, 23, 81, 77, 24, 10 })); // works for (int i = 0; i != 2; ++i) { for (const auto& e : *(p + i)) std::cout << e << ", "; std::cout << std::endl; } a.destroy(p); a.destroy(p + 1); a.deallocate(p, 10); std::cout << std::endl; } 的构造函数是print_vec,那么为什么调用vector(size_type)有效呢?只要将此值explicit传递到vector的构造函数中,即可创建5个值初始化的整数元素。

  • 第二,我可以调用a.construct(p, 5);并传入5(vector(initializer_list)不是显式ctor),但是为什么该语句不能编译:{{1​​}}? p>

0 个答案:

没有答案