How Can I Prevent Template Generation for Objects

2019-02-18 22:22发布

So for the purpose of example let's say I have 3 simple structs, the second of which doesn't contain a bar method:

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

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

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

Then I have a struct which will manage objects of these types:

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, [&](auto& value) { value.bar(); } ); }
};

When I try to compile this I get:

error: struct two has no member named bar

Is there a way that I can work around this?

Live Example

1条回答
我只想做你的唯一
2楼-- · 2019-02-18 22:36

First, the utilities. One is our favorite overload that shows off three C++17 features, and overloads the operator() of several function objects, all in two lines.

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

Then an accepts-everything fallback tag type:

struct fallback_t { template<class T> fallback_t(T&&) {} };

Now to the call itself. We make the original generic lambda SFINAE-friendly by putting the value.bar() call into the trailing return type, then overload it with a fallback overload that has undefined behavior if actually invoked (since OP "omit[ted] any explicit definition of behavior"):

void bar(const int key) { 
    findObject(key, overload {
          [&](auto& value) -> decltype(void(value.bar())) { value.bar(); },
          [](fallback_t){ fire_missiles_and_impregnate_cat(); }
    } ); 
}
查看更多
登录 后发表回答