if-else取决于T是否是完整类型

jav*_*ver 7 c++ templates incomplete-type c++14

如何检查某种类型是否是某种类型的完整类型.cpp

template<class T>class Test{
    //some fields
    void(*functor)(T*) =[](T*){}; 
    //^ will be written by some .cpp that can access T as complete-type 
    T* t=nullptr;
    void fComplete(){    
        delete t;     //faster
        /** ^ some code that use complete type*/    
    }
    void fForward(){
        functor(t);   //slower
        /** ^ some code that forward declaration is enough*/   
    }
    void f(){  
        /*if(T is complete type){    
            fComplete();
        }else fForward();*/
    }
};
Run Code Online (Sandbox Code Playgroud)

演示

当我想在我的自定义智能指针中过早优化删除功能时,它会很有用.

任何人都可以确认这是不可能的吗?
我不是要求解决方法(但我不介意) - 这个问题只是我的好奇心.

Cur*_*ous 7

这有效

#include <iostream>
#include <type_traits>

using namespace std;

class Incomplete;
class Complete {};

template <typename IncompleteType, typename = std::enable_if_t<true>>
struct DetermineComplete {
    static constexpr const bool value = false;
};

template <typename IncompleteType>
struct DetermineComplete<
        IncompleteType,
        std::enable_if_t<sizeof(IncompleteType) == sizeof(IncompleteType)>> {
    static constexpr const bool value = true;
};

int main() {
    cout << DetermineComplete<Complete>::value << endl;
    cout << DetermineComplete<Incomplete>::value << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

注意我喜欢使用std::enable_if_t相同的效果,void_t直到可用,而不是自己在任何地方编写它的实现.

注意请查看其他答案以及ODR.它们提出了一个在使用它之前应该考虑的有效点.