sfinae 实例化一个函数体吗? [英] does sfinae instantiates a function body?

查看:29
本文介绍了sfinae 实例化一个函数体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用通常的 SFINAE 技巧检测类的特定成员函数是否存在.

I want to detect existence of a specific member function for a class, using the usual SFINAE trick.

template<typename T>
struct has_alloc
{
    template<typename U,U x>
    struct dummy;

    template<typename U>
    static char test(dummy<void* (U::*)(std::size_t),&U::allocate>*);
    template<typename U>
    static char (&test(...))[2];
    static bool const value = sizeof(test<T>(0)) ==1;
};

应该注意的是,这检测到一种不同类型的分配器,它具有 void* allocate(std::size_t) 作为非标准成员函数(可能是一些原始内存分配器).

It should be noted that this detects a different kind of allocator which has void* allocate(std::size_t) as member function which are non standard (probably some raw memory allocator).

接下来,我有一个不完整的类型和一个用于该不完整类型的 std::allocator.

Next, I have an incomplete type and an std::allocator for that incomplete type.

struct test;
typedef std::allocator<test> test_alloc;

我正在检查 test_alloc 是否就是我要找的那个.

And I am checking whether the test_alloc is the one I am looking for.

struct kind_of_alloc
{
   const static bool value =  has_alloc<test_alloc>::value;
};

当我使用"test_alloc 时,struct test 肯定会完成,例如

Surely struct test will be complete when I will "use" test_alloc such as

#include "test_def.hpp"//contains the code posted above
struct test{};

void use()
{
    test_alloc a;    
}

在另一个编译单元中.然而,当 has_alloc 测试发生时,编译器尝试实例化 std::allocator 的分配函数,发现函数体内部使用了不完整类型的 sizeof,并导致硬错误.如果在使用时将allocate的实现分开并单独包含,则似乎不会发生错误,例如

in another compilation unit. However when the has_alloc test happens,the compiler tries to instantiate the allocate function for std::allocator and finds that sizeof an incomplete type is used inside function body, and causes a hard error. It seems that the error doesn't occur if the implementation of allocate separated and included separately at the point of use such as

template<typename T>
T* alloc<T>::allocate(std::size_t n)
{
      return (T*)operator new(sizeof(T)*n);
}
void use()
{
    test_alloc a;
    a.allocate(2);
}

并且test_def.hpp包含

template<typename T>
struct alloc
{
  T* allocate(std::size_t n);
};

然而,虽然我可以为 alloc 做到这一点,但对于 std::allocator 来说这是不可能的,因为分离实现是不可能的.我正在寻找的是可以测试 test_alloc 中是否存在带有 void* allocate(size_t) 的函数.如果不是,它将测试为阴性,如果是,即如果函数签名匹配,即使它不能在那里实例化,测试肯定.

However while I can do this for alloc<T> , it is not possible for std::allocator as separating out the implementation is not possible. What I am looking for is it possible to test whether a function with void* allocate(size_t) exists in test_alloc. If not, it will test negative, and if yes ,i.e. if the function signature matches, even if it can not be instantiated there, test positive.

推荐答案

否,SFINAE 仅在重载解决期间有效.一旦做出决议并且编译器开始实例化函数 SFINAE 就结束了.

No, SFINAE is only in effect during overload resolution. Once a resolution has been made and the compiler begins instantiating the function SFINAE is over.

并获取一个函数的地址来实例化它.

and taking the address of a function instantiates it.

这篇关于sfinae 实例化一个函数体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆