使用不同的返回类型强制SFINAE

问题描述 投票:0回答:1

所以this answer演示了如何在返回类型中使用函数来强制Substitution Failure is not an Error (SFINAE)

有没有办法可以使用它并从函数中获得不同的返回类型?

所以这个与overload相关的参数将触发SFINAE:

overload {
    [&](auto& value) -> decltype(void(value.bar())) { value.bar(); } ,
    [](fallback_t) { cout << "fallback\n"; }
}

但是让我说我​​需要一个不同的返回类型,我怎么还能触发SFINAE?例如,我想做这样的事情:

overload {
    [&](auto& value) -> decltype(void(value.bar()), float) { value.bar(); return 1.0F; } ,
    [](fallback_t) { cout << "fallback\n"; return 13.0F; }
}


这是最小,完整,可验证的示例。这是很多东西要读,但是如果你不想查看链接,这是我尝试添加返回类型之前涉及的代码:

struct one {
    void foo(const int);
    void bar();
};

struct two {
    void foo(const int);
};

struct three {
    void foo(const int);
    void bar();
};

template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
struct fallback_t { template<class T> fallback_t(T&&) {} };

struct owner {
    map<int, one> ones;
    map<int, two> twos;
    map<int, three> threes;

    template <typename T, typename Func>
    void callFunc(T& param, const Func& func) {
        func(param);
    }

    template <typename T>
    void findObject(int key, const T& func) {
        if(ones.count(key) != 0U) {
            callFunc(ones[key], func);
        } else if(twos.count(key) != 0U) {
            callFunc(twos[key], func);
        } else {
            callFunc(threes[key], func);
        }
    }

    void foo(const int key, const int param) { findObject(key, [&](auto& value) { value.foo(param); } ); }
    void bar(const int key) {
        findObject(key, overload {
            [&](auto& value) -> decltype(void(value.bar())) { value.bar(); } ,
            [](fallback_t) { cout << "fallback\n"; }
        } );
    }
};

int main() {
    owner myOwner;

    myOwner.ones.insert(make_pair(0, one()));
    myOwner.twos.insert(make_pair(1, two()));
    myOwner.threes.insert(make_pair(2, three()));

    myOwner.foo(2, 1);
    cout << myOwner.bar(1) << endl;
    cout << myOwner.bar(2) << endl;
    cout << myOwner.foo(0, 10) << endl;
}
c++ templates lambda sfinae return-type
1个回答
1
投票

你想要你的吗?

[&](auto& value) -> decltype(void(value.bar())) { value.bar(); } ,

返回浮动而不是空白?那么为什么不将float添加到decltype?

[&](auto& value) -> decltype(void(value.bar()), 1.0f) { value.bar(); return 1.0; }
© www.soinside.com 2019 - 2024. All rights reserved.