2017-02-21 12 views
3

을 필요로 하는가? 다음 예에서왜 범위-V3 수율 내가 어떤 이유로 기능의 항복 가족이 클래스는 기본 작도 할 필요합니까, 이해하려고 노력하고 기본 생성자

는 vnums1 라인은 CNum는 기본 생성자가있는 경우에만 컴파일합니다. vnums2 행에는 기본 생성자가 필요하지 않습니다.

나는 비주얼 스튜디오 2017과 범위-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을 필요로하지 않도록 코드를 변경했습니다. 젠장 당겨 즐길 수 있습니다.

+0

좋습니다. 고맙습니다. 마이크로 소프트/레인지-V3-VS2015의 메인테이너 ericniebler/범위 - V3에서 새로운 변화를 따기되지 않습니다. 누구든지 VC++ 2017에서 최신 비트를 구할 수있는 방법에 대한 제안이 있습니까? – CodeAndLearn

+0

는 슬프게도, 당신의 최선의 방법은 마이크로 소프트/레인지-V3-VS2015의 REPO 포크하고 변경 사항을 직접 확인하는 것입니다. –

1

ranges::yield_if를 사용하는 생성자를 기본적으로해야 할 이유는 사용하는 기계는 기본 constructable로 유형을 필요로한다는 것이다. 우리가 가진 코드를 보면

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이 필요한 유형을 설명하며

기본값이 생성 가능하고 복사 가능하며 이동 가능하고 생성 가능해야하며 destructable이어야한다고 설명합니다.