Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

c++17 - C++: static assert that functor's argument is const reference

I'm writing a template function in C++17 which accepts a functor F as argument and I want to restrict the passed in functor to have only one constant reference argument, where T can be any type.

for example:

template <class T> struct my_struct{
    std::vector<T> collection;
    template <class F> std::vector<T> func(F f){
        static_assert(
               // CONDITION HERE!,
               "f's argument is not const reference"
            );

        std::vector<T> ret;

        std::copy_if(
                std::make_move_iterator(this->collection.begin()),
                std::make_move_iterator(this->collection.end()),
                std::inserter(ret, ret.end()),
                f
            );

        return ret;
    }
};

Obviously, in case f is [](auto v){return true;} the resulting vector returned from func will have empty elements (because those are moved before adding to resulting container). So, I need to restrict possible input functors to [](const auto& v){}.

I have tried something like this:

static_assert(
        std::is_invocable_v<F, const T&>,
        "f's argument is not const reference"
    );

But then func([](auto v){}) does not trigger the assertion, because T is copyable in my case. The func([](auto& v){}) also passes the test, because auto can be const T.

But I need to limit possible lambdas to be func([](const auto& v){}).

question from:https://stackoverflow.com/questions/65888788/c-static-assert-that-functors-argument-is-const-reference

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might write traits (with its limitations), something like:

template <typename Sig> struct callable_traits;

template <typename Ret, typename ...Args>
struct callable_traits<Ret(*)(Args...)>
{
    using args = std::tuple<Args...>;
};
// add specialization for C-ellipsis too

template <typename Ret, class C, typename ...Args>
struct callable_traits<Ret(C::*)(Args...) const>
{
    using args = std::tuple<Args...>;
};
// add specialization for variant with C-ellipsis, cv-qualifier, ref-qualifier

template <class C>
struct callable_traits<C> : callable_traits<&C::operator()>{};

Limitation of the traits: doesn't handle templated operator() (as for generic lambda), overloaded operator().

And then

template <class T> struct my_struct{
    template <class F> void func(F f){
        static_assert(
               std::is_same_v<std::tuple<const T&>, typename callable_traits<F>::args>,
               "f's argument is not const reference"
            );

        // here goes some code which can possibly call f with rvalue
        // reference argument, so I want to avoid situation when the
        // argument object is moved or modified. I don't have control
        // over this code because it an STL algorithm.
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...