相关疑难解决方法(0)

如何在C++ 11,C++ 11,14和17之前简化复杂的SFINAE语法?

这个问题的灵感来自于这个答案.我想知道在给定标准中简化它的最佳方法是什么.一个我知道并且个人使用/仍在使用,因为C++ 14是宏REQUIRES(x):

定义:

template<long N>
struct requires_enum
{
    enum class type
    {
        none,
        all
    };
};

#define REQUIRES(...) requires_enum<__LINE__>::type = \
                      requires_enum<__LINE__>::type::none, \
                      bool PrivateBool = true, \
                      typename std::enable_if<PrivateBool && (__VA_ARGS__), int>::type = 0
Run Code Online (Sandbox Code Playgroud)

即使对于非模板化函数调用也使用:

template<REQUIRES(sizeof(int)==4)>
int fun() {return 0;}

int main()
{ 
    fun(); //only if sizeof(int)==4
}
Run Code Online (Sandbox Code Playgroud)

REQUIRES我使用的原文来自这篇文章.

还有什么好的技巧?


SFINAE的一些例子需要一些或很长时间才能理解读者刚开始使用SFINAE的冒险:

Pre-C++ 11 SFINAE示例(来源):

template <typename T>
struct has_typedef_foobar {
    // Types "yes" and "no" are guaranteed to have different …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++03 c++17

12
推荐指数
1
解决办法
1108
查看次数

如何在检测成语中要求精确的函数签名?

假设我有一个类型T,我想检测它是否有一个下标操作符,我可以用另一种类型调用它Index.以下示例工作得很好:

#include <type_traits>
#include <vector>

template < typename T, typename Index >
using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);

int main()
{
    using a = subscript_t< std::vector<int>, size_t >;
    using b = subscript_t< std::vector<int>, int    >;
}
Run Code Online (Sandbox Code Playgroud)

但是,当且仅当函数签名完全匹配时,我希望检测函数.在上面的例子中,我希望语句subscript_t< std::vector<int>, int >;抛出一个错误no viable overloaded operator[],因为下标运算符的签名std::vector

std::vector<T, std::allocator<T>>::operator[](size_type pos);
Run Code Online (Sandbox Code Playgroud)

size_typeGCC 在哪里unsigned long.如何避免隐式转换从intsize_t要发生?

c++ templates types type-conversion c++11

4
推荐指数
1
解决办法
716
查看次数

标签 统计

c++ ×2

templates ×2

c++03 ×1

c++11 ×1

c++17 ×1

sfinae ×1

type-conversion ×1

types ×1