«-Werror = subobject-linkage» для лямбды в качестве параметра шаблона в заголовке: что является причиной для отклонения этого? - PullRequest
0 голосов
/ 27 апреля 2019

Продолжение для этого вопроса .

Учитывая этот код в header.hxx:

template <class Func> class ScopeGuard
{
public:
    /** @param func function object to be executed in dtor
    */
    explicit ScopeGuard( Func && func ) : m_func( std::move(func) ) {}

    ~ScopeGuard()
    {
        if (m_bDismissed)
            return;
        m_func();
    }

    /** Dismisses the scope guard, i.e. the function won't
        be executed.
    */
    void dismiss() { m_bDismissed = true; }

private:
    // noncopyable until we have good reasons...
    ScopeGuard(const ScopeGuard&) = delete;
    ScopeGuard& operator=(const ScopeGuard&) = delete;

    Func m_func;
    bool m_bDismissed = false;
};

// Get functor for cleanup to use in FlagRestorationGuard
auto GetFlagRestorationGuard(bool& i_flagRef)
{
    return [&i_flagRef, resetVal = i_flagRef] { i_flagRef = resetVal; };
}

class FlagRestorationGuard : public ScopeGuard<decltype(GetFlagRestorationGuard(std::declval<bool&>()))>
{
public:
    FlagRestorationGuard( bool& i_flagRef, bool i_temporaryValue )
        : ScopeGuard(GetFlagRestorationGuard(i_flagRef))
    {
        i_flagRef = i_temporaryValue;
    }
};

и этот код в file.cxx:

#include <cassert>
#include "header.hxx"

void foo()
{
    bool bFlag = false;
    {
        // Test that comphelper::FlagRestorationGuard properly sets and resets the flag
        comphelper::FlagRestorationGuard aGuard(bFlag, true);
        assert(bFlag);
    }
    assert(!bFlag);
}

выдает эту ошибку:

error: ‘comphelper::FlagRestorationGuard’ has a field ‘comphelper::FlagRestorationGuard::<anonymous>’ whose type uses the anonymous namespace [-Werror=subobject-linkage]
     class FlagRestorationGuard
           ^~~~~~~~~~~~~~~~~~~~

Я вижу, что лямбда, конечно, является неназванным объектом, используя неявное безымянное пространство имен; но насколько это актуально в этом случае? Почему этот диагностический блок должен что-либо здесь блокировать?

Ссылка на код.

Ссылка на ошибку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...