美文网首页程序员
C++奇淫巧技之SFINAE

C++奇淫巧技之SFINAE

作者: 小聪明李良才 | 来源:发表于2017-03-22 10:59 被阅读2697次

SFINAE 技术,即匹配失败不是错误,英文Substitution Failure Is Not An Error,其作用是当我们在进行模板特化的时候,会去选择那个正确的模板,避免失败

看个具体的例子:

long multiply(int i, int j) { return i * j; }

template <class T>
typename T::multiplication_result multiply(T t1, T t2)
{
    return t1 * t2;
}
int main(void)
{
    multiply(4,5);
}

当我们编译的时候,会去匹配模板 multiply,但是由于我们不知道multiplication_result,根据 Substitution Failure Is Not An Error ,于是我们就去选择函数 multiply

这种技术在代码中的一个大的用途就是在编译时期来确定某个 type 是否具有我们需要的性质,看代码

template <class T>
struct is_pointer
{
    template <class U>
    static char is_ptr(U *);

    template <class X, class Y>
    static char is_ptr(Y X::*);

    template <class U>
    static char is_ptr(U (*)());

    static double is_ptr(...);

    static T t;
    enum { value = sizeof(is_ptr(t)) == sizeof(char) };
};

struct Foo {
    int bar;
};

void testTypeCheck() {
    typedef int * IntPtr;
    typedef int Foo::* FooMemberPtr;
    typedef int (*FuncPtr)();

    printf("%d\n",is_pointer<IntPtr>::value);        // prints 1
    printf("%d\n",is_pointer<FooMemberPtr>::value);  // prints 1
    printf("%d\n",is_pointer<FuncPtr>::value);       // prints 1
}

通过定义4个重载的 is_ptr函数,3个是接受不同的指针参数,另一个则包括了其他的所有参数,
IntPtr 是一个变量指针
FooMemberPtr 是一个成员属性指针
FuncPtr 是一个函数指针

接着我们来看下 muduo 库中的一段代码:

template<typename T>
struct has_no_destroy {
    template<typename C>
    static char test(decltype(&C::no_destroy));


    template<typename C>
    static int32_t test(...);

    const static bool value = sizeof(test<T>(0)) == 1;
};
// 其作用就是用来判断是否有 no_destroy 函数

struct A {

};

struct B {
    void no_destroy(){}
};
struct C {
    int no_destroy;
};

struct D : B {

};

void testNoDestroy() {
    printf("%d\n",has_no_destroy<A>::value);
    printf("%d\n",has_no_destroy<B>::value);
    printf("%d\n",has_no_destroy<C>::value);
    printf("%d\n",has_no_destroy<D>::value);
}

其作用主要是判断是否有no_destroy,并且在继承上也成立,但是继承在不同的gcc版本上不一定成立,具体可以看:http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions

code地址:https://github.com/zhuanxuhit/happy-code/tree/master/src/idioms

相关文章

  • C++奇淫巧技之SFINAE

    SFINAE 技术,即匹配失败不是错误,英文Substitution Failure Is Not An Erro...

  • C++奇淫巧技汇总

    众所周知,Cartographer源码中使用大量c++11特性,为了更加丝滑地读代码,本文将记录源码阅读过程中遇到...

  • 奇淫巧技

    input type=file 手机浏览器打开相机 input 元素type=file时 ios内核浏览器默认事件...

  • Java奇淫巧技之 Lombok

    lombok是一个通过简单的 注解 的形式来简化消除一些必须但显得很臃肿的 Java 代码的工具 简单来说,比如我...

  • Java奇淫巧技之Lombok

    Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应...

  • Java奇淫巧技之Lombok

    http://blog.csdn.net/ghsau/article/details/52334762https:...

  • 开发奇淫巧技

    1.手机在脱离xcode后,重新连接数据线,打开 Debug-attachToProcess,把当前运行进程关联上...

  • JS 奇淫巧技

    作者 | Before Semicolon 译者 | 王强 来源 | 前端之巅 写代码的时候总有一些东西是会重...

  • Mysql的奇淫巧技

    ``` ###查看时区 show variables like '%time_zone%'; ###日期转为时间戳...

  • 面试奇淫巧技之——自我介绍

    众所周知,自我介绍一般是面试的第一环节,所以在面试中给面试官留下良好的印象是非常有必要的,一定要把个人优势与职位要...

网友评论

    本文标题:C++奇淫巧技之SFINAE

    本文链接:https://www.haomeiwen.com/subject/qwxonttx.html