2017-02-21 110 views
3

我想了解,爲什麼產量系列函數要求該類是默認可構造的?爲什麼範圍-V3產量需要默認構造函數

在以下示例中,僅當CNum具有默認構造函數時,vnums1行纔會編譯。 vnums2行不需要默認構造函數。

我正在使用Visual Studio 2017和Range-V3-VS2015。謝謝!

#include <range/v3/all.hpp> 

struct CNum 
{ 
    // CNum() = default; 
    explicit CNum(int num) : m_num(num) {} 
    int m_num; 
}; 

int main() 
{ 
    auto ints = ranges::view::ints(0, 10); 

    // this compiles only of CNum has a default constructor 
    auto vnums1 = ints 
     | ranges::view::for_each([](int num) { return ranges::yield_if(num % 2, CNum(num)); }) 
     | ranges::to_vector; 

    // this compiles even if CNum does not have a default constructor 
    auto vnums2 = ints 
     | ranges::view::remove_if([](int num) { return num % 2 == 0; }) 
     | ranges::view::transform([](int num) { return CNum(num); }) 
     | ranges::to_vector; 

    return 0; 
} 

回答

2

我們只是將代碼更改爲不需要DefaultConstructible。 git拉和享受。

+0

太好了。謝謝。 Microsoft/Range-V3-VS2015的維護人員沒有從ericniebler/range-v3中獲得新的更改。有沒有人對我如何才能獲得與VC++ 2017的最新工作有關的建議? – CodeAndLearn

+0

不幸的是,您最好的選擇是分叉Microsoft/Range-V3-VS2015回購並自行進行更改。 –

1

你需要默認的構造函數使用ranges::yield_if的原因是,它使用的機器需要類型爲默認施工的。如果我們看看我們的代碼

struct yield_if_fn 
{ 
    template<typename V> 
    repeat_n_view<V> operator()(bool b, V v) const 
    { 
     return view::repeat_n(std::move(v), b ? 1 : 0); 
    } 
}; 

/// \relates yield_if_fn 
/// \ingroup group-views 
RANGES_INLINE_VARIABLE(yield_if_fn, yield_if) 

而且我們可以看到它叫view::repeat_n。看着這些代碼我們得到

repeat_n_view<Val> operator()(Val value, std::ptrdiff_t n) const 
{ 
    return repeat_n_view<Val>{std::move(value), n}; 
} 

如果我們看一下repeat_n_view我們

// Ordinarily, a view shouldn't contain its elements. This is so that copying 
// and assigning ranges is O(1), and also so that in the event of element 
// mutation, all the copies of the range see the mutation the same way. The 
// repeat_n_view *does* own its lone element, though. This is OK because: 
// - O(N) copying is fine when N==1 as it is in this case, and 
// - The element is immutable, so there is no potential for incorrect 
// semantics. 

struct repeat_n_view 
    : view_facade<repeat_n_view<Val>, finite> 
{ 
private: 
    friend range_access; 
    Val value_; 
    std::ptrdiff_t n_; 

    // ... 
public: 
    repeat_n_view() = default; 
    constexpr repeat_n_view(Val value, std::ptrdiff_t n) 
     : value_(detail::move(value)), n_((RANGES_EXPECT(0 <= n), n)) 
    {} 
    constexpr std::size_t size() const 
    { 
     return static_cast<std::size_t>(n_); 
    } 
}; 

我們在徵求意見,這是一個設計決定,因爲這種設計的看你需要你的類型爲默認可構造。 Eric描述了所需的類型爲SemiRegular,其被記錄爲

它需要是默認可構造的,複製和移動可構造的,並且可破壞的。

相關問題