用于检测模板函数的 Sfinae 类型特征不适用于 std::forward



我最近尝试创建一个finae类型trait来检测一个类是否包含一个名为construct的特定模板静态函数。

我带来了这个实现:

template<typename T, typename... Args>
struct has_template_construct_helper {
private:
    template<typename U, typename... As>
    static std::true_type test(decltype(&U::template construct<As...>)*);
    template<typename...>
    static std::false_type test(...);
public:
    using type = decltype(test<T, Args...>(nullptr));
};
template<typename T, typename... Args>
using has_template_construct = typename has_template_construct_helper<T, Args...>::type;

我以为会没事的,事实也的确如此。我试着用gcc和clang测试我的特性,像这样:

struct TestStruct {
    template<typename... Args>
    static auto construct(int a, double b, Args... args) -> decltype(std::make_tuple(a, b, args...)) {
        return std::make_tuple(1, 2.3, std::forward<Args>(args)...);
    }
};
// didn't fire! Hurrah!
static_assert(has_template_construct<TestStruct, std::string>::value, "Don't pass the test");

它在两个编译器中都有效。

然而,当我添加转发引用时,clang开始抱怨:

struct TestStruct {
    template<typename... Args>
    static auto construct(int a, double b, Args&&... args) -> decltype(std::make_tuple(a, b, std::forward<Args>(args)...))
    {
        return std::make_tuple(1, 2.3, std::forward<Args>(args)...);
    }
};
// fires on clang :(
static_assert(has_template_construct<TestStruct, std::string>::value, "Don't pass the test");

下面是coliru上的代码片段:GCC, Clang

我的问题是:GCC和Clang之间哪一个是错误的,我怎么能修复我的代码,使它在两个编译器上工作?


好吧,我试过了,现在我更困惑了。当使用std::declval时,它可以在clang中工作!

struct TestStruct {
    template<typename... Args>
    static auto construct(int a, double b, Args&&... args) -> decltype(std::make_tuple(a, b, std::declval<Args>()...))
    {
        return std::make_tuple(1, 2.3, std::forward<Args>(args)...);
    }
};
// uh?? Works in clang?
static_assert(has_template_construct<TestStruct, std::string>::value, "Don't pass the test");

我不太确定为什么你的代码在clang++中失败(或在g++中传递)。但这里有一个更简单的选择。

#include <type_traits>
#include <tuple>
#include <string>
template <typename... T>
using void_t = void;
class Stat {
public:
    template <typename... T>
    static auto construct(int a, double b, T&&... t) ->
      decltype(std::make_tuple(1, 2.3, t...))
    {
      return std::make_tuple(1, 2.3, std::forward<T>(t)...);
    }
};
template <typename Class, typename... Args>
constexpr auto does_have_construct(int)
    -> decltype(&Class::template construct<Args...>, true)
{
    return true;
}
template <typename Class, typename... Args>
constexpr bool does_have_construct(long) { return false; }
class Stat2 {};
int main() {
    static_assert(does_have_construct<Stat, std::string>(0), "Nope!");
    return 0;
}

Clang特别不高兴在返回类型演绎的decltype中指定std::forward<T>。如果我们去掉它,就没有问题了。但是,我现在不确定代码的正确性!!

在c++ 14中,您可以将class Stat重写为:

class Stat {
public:
    template <typename... T>
    static auto construct(int a, double b, T&&... t)
    {
      return std::make_tuple(1, 2.3, std::forward<T>(t)...);
    }
};

如您所见,在这种情况下,我们不必采取额外的步骤来欺骗编译器。

最新更新