簡體   English   中英

std :: launder alternative pre c ++ 17

[英]std::launder alternative pre c++17

它就像std::optional ,但不存儲額外的bool。 用戶必須確保僅在初始化后訪問。

template<class T>
union FakeOptional { //Could be a normal struct in which case will need std::aligned storage object.
    FakeOptional(){}  //Does not construct T
    template<class... Args>
    void emplace(Args&&... args){
        new(&t) T{std::forward<Args&&>(args)...};
    }
    void reset(){
        t.~T();
    }
    operator bool() const {
        return true;
    }
    constexpr const T* operator->() const {
        return std::launder(&t);

    }
    constexpr T* operator->() {
        return std::launder(&t);
    }
    T t;
};

如果您想知道為什么我需要這樣一個模糊的數據結構,請點擊此處: https//gitlab.com/balki/linkedlist/tree/master

  1. 可以忽略std::launder嗎? 我猜不會。
  2. 由於std::launder僅在c ++ 17中可用,如何在c ++ 14中實現上面的類? boost::optionalstd::experimental::optional應該需要類似的功能還是使用編譯器特定的魔法?

注意:很容易遺漏,類型被聲明為union 這意味着T構造函數實際上沒有被調用。 參考: https//gcc.godbolt.org/z/EVpfSN

不,你不能。 提出std::launder的原因之一是std::optional在C ++ 14中無法實現。 您可以參考此討論以獲取詳細信息。

另一方面,您可以在沒有constexpr情況下實現一個。 我們的想法是使用一個緩沖區reinterpret_cast ,因為結果reinterpret_cast總是引用到新創建的對象(在C ++ 17 std::launder仍然是必需的,但在C ++中14,這是好的)。 例如,

template<class T>
struct FakeOptional { 
    FakeOptional(){}  
    template<class... Args>
    void emplace(Args&&... args){
        new(&storage) T{std::forward<Args&&>(args)...};
    }
    void reset(){
        reinterpret_cast<T*>(&storage)->~T();
    }
    operator bool() const {
        return true;
    }
    const T* operator->() const {
        return reinterpret_cast<const T*>(&storage);
    }
    T* operator->() {
        return reinterpret_cast<T*>(&storage);
    }
    std::aligned_storage_t<sizeof(T), alignof(T)> storage;
};

boost::optional的實現使用了這個想法 ,並沒有實現constexpr語義(您可以參考其源代碼了解詳細信息)。

std::tie 用於 C++17 結構運算符<!--?</div--><div id="text_translate"><p> 考慮以下 C++17 結構:</p><pre> struct S { M1 m1; M2 m2; M3 m3; bool operator<(const S& that) const { return tie() < that.tie(); } auto tie() const { return std::tie(m1,m2,m3); } };</pre><p> 這個對嗎? S::tie會返回成員引用的元組,還是會復制一份? 會自動推斷出正確的類型(引用元組)嗎? constness 做正確的事嗎?</p><p> (我看到的示例對 std::tie 進行了兩次調用,並且沒有像這樣分解為單獨的成員 function。想知道/懷疑是否有原因。)</p></div>

[英]std::tie for C++17 struct operator<?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM