C++11 特化可变参数函数的一个版本

C++11 Specialize one version of variadic function

我正在尝试创建一个可变参数函数,它接受任意数量的参数,但我想专门说明只传入两个带迭代器的参数的情况。传入两个非迭代器参数,应该仍然使用通用可变版本。我遇到了无法克服的 static_assert 失败。它似乎试图评估 with_iterator_args 中的整个表达式,如果函数少于两个参数,它会失败,而不是在对 2 个参数的检查已经产生 false 时跳过对余数的评估。

有没有办法在不为一个和两个参数的情况添加两个重载的情况下做到这一点?

这是我目前拥有的:

#include <iostream>
#include <vector>
#include <tuple>

// inspired by 
template <typename... Args>
struct args_traits
{
    enum { arity = sizeof...(Args) };

    template <size_t i>
    struct arg
    {
        typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
    };
};

// based on: 
template <typename T>
struct is_iterator
{
    static char test(...);

    template <typename U,
        typename=typename std::iterator_traits<U>::difference_type,
        typename=typename std::iterator_traits<U>::pointer,
        typename=typename std::iterator_traits<U>::reference,
        typename=typename std::iterator_traits<U>::value_type,
        typename=typename std::iterator_traits<U>::iterator_category
    > static long test(U&&);

    constexpr static bool value = std::is_same<decltype(test(std::declval<T>())),long>::value;
};

template<typename Arg1, typename Arg2>
struct is_iterator_args
{
    constexpr static bool value = is_iterator<Arg1>::value && is_iterator<Arg2>::value;
};

template<typename... Args>
struct with_iterator_args
{
    constexpr static bool value = args_traits<Args...>::arity == 2
        && is_iterator_args<typename args_traits<Args...>::template arg<0>::type, typename args_traits<Args...>::template arg<1>::type>::value;
};

template <typename T, typename... Args,
    typename = typename std::enable_if<!with_iterator_args<Args...>::value>::type>
void some_func(T first, Args&&... args)
{
    std::cout << "func(" << first << ") called with " << sizeof...(args) << " args" << std::endl;
}

template <typename T, typename Begin, typename End,
    typename = typename std::enable_if<is_iterator_args<Begin, End>::value>::type>
void some_func(T first, Begin begin, End end)
{
    std::cout << "func(" << first << ") called with iterators: " << std::distance(begin, end) << std::endl;
}

int main()
{
    std::vector<int> v{1, 2, 3};

    some_func(1, v.begin(), v.end()); // special case, using iterators
    some_func(1, "arg2", 3, std::string("arg4"));
    some_func(1, "arg2");
    some_func(1);
    some_func(1, "arg2", 3, std::string("arg4"), 5.67);
    return 0;
}

这是失败的地方:

In file included from test.cpp:3:
/usr/include/c++/9/tuple: In instantiation of ‘struct std::tuple_element<0, std::tuple<> >’:
/usr/include/c++/9/tuple:1285:12:   required from ‘struct std::tuple_element<1, std::tuple<const char (&)[5]> >’
test.cpp:14:69:   required from ‘struct args_traits<const char (&)[5]>::arg<1>’
test.cpp:45:3:   required from ‘constexpr const bool with_iterator_args<const char (&)[5]>::value’
test.cpp:49:37:   required by substitution of ‘template<class T, class ... Args, class> void some_func(T, Args&& ...) [with T = int; Args = {const char (&)[5]}; <template-parameter-1-3> = <missing>]’
test.cpp:68:21:   required from here
/usr/include/c++/9/tuple:1303:25: error: static assertion failed: tuple index is in range
 1303 |       static_assert(__i < tuple_size<tuple<>>::value,
      |                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

我似乎只能通过添加更多重载而不是使用 with_iterator_args:

来使其工作
template <typename T, typename... Args>
void some_func_common(T first, Args&&... args)
{
    std::cout << "func(" << first << ") called with " << sizeof...(args) << " args" << std::endl;
}

template <typename T, typename A>
void some_func(T first, A arg)
{
    some_func_common(first, arg);
}

template <typename T>
void some_func(T first)
{
    some_func_common(first);
}

template <typename T, typename A1, typename A2, typename... Args,
    typename = typename std::enable_if<!is_iterator_args<A1, A2>::value>::type>
void some_func(T first, A1 begin, A2 end, Args&&... args)
{
    some_func_common(first, std::forward<A1>(begin), std::forward<A2>(end), std::forward<Args>(args)...);
}

template <typename T, typename Begin, typename End,
    typename = typename std::enable_if<is_iterator_args<Begin, End>::value>::type>
void some_func(T first, Begin begin, End end)
{
    std::cout << "func(" << first << ") called iterators: " << std::distance(begin, end) << std::endl;
}

不过,它似乎不必要地混乱。

你的问题是 && 之前和之后的表达式都必须编译 - 即使 && 之后的表达式不会被使用。

我的第一个尝试是利用 C++17 constexpr if

template<typename... Args>
struct with_iterator_args
{
private:
    constexpr static bool value_checker() {
        if constexpr (args_traits<Args...>::arity == 2) {
            return is_iterator_args<typename args_traits<Args...>::template arg<0>::type, typename args_traits<Args...>::template arg<1>::type>::value;
        }
        else {
            return false;
        }
    }

public:
    constexpr static bool value = value_checker();
};

如果你需要坚持使用 C++11,你可以使用 std::conditional。请注意,我还使用 std::false_typestd::true_type

template<typename... Args>
struct is_iterator_args :
    std::conditional<is_iterator<typename args_traits<Args...>::template arg<0>::type>::value &&
                     is_iterator<typename args_traits<Args...>::template arg<1>::type>::value,
                    std::true_type, std::false_type>::type
{
};

template<typename... Args>
struct with_iterator_args :
     std::conditional<sizeof...(Args) == 2, is_iterator_args<Args...>, std::false_type>::type
{
};