MSVS 2010 では、組み込みの型ポインターで実行するとエラーと見なされるため、SFINAE を使用してデストラクターを明示的に呼び出すのをやめたいと考えています。
どうすればいいですか?
MSVS 2010 では、組み込みの型ポインターで実行するとエラーと見なされるため、SFINAE を使用してデストラクターを明示的に呼び出すのをやめたいと考えています。
どうすればいいですか?
間違った角度から見るかもしれません: 機能しないものを除外するべきではなく、機能するものを検出する必要があります。あなたの場合、特定の型T
がクラスであるかどうかを確認しようとしているため、デストラクタを呼び出すことができます。
そうは言っても、あなたはstd::is_class
. お使いのコンパイラで利用できない場合は、VC++ 8 以降で動作するBoost.TypeTraitsを利用できます。boost::is_class
With the following type function we can determine whether a type is a class type:
// traits/isclasst.hpp
template<typename T>
class IsClassT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(…);
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};
This template uses SFINAME principle.
The following program uses this type function to test whether certain types and objects are class types:
// traits/isclasst.cpp
#include <iostream>
#include "isclasst.hpp"
class MyClass {
};
struct MyStruct {
};
union MyUnion {
};
void myfunc()
{
}
enumE{e1}e;
// check by passing type as template argument
template <typename T>
void check()
{
if (IsClassT<T>::Yes) {
std::cout << " IsClassT " << std::endl;
}
else {
std::cout << " !IsClassT " << std::endl;
}
}
// check by passing type as function call argument
template <typename T>
void checkT (T)
{
check<T>();
}
int main()
{
std::cout << "int: ";
check<int>();
std::cout << "MyClass: ";
check<MyClass>();
std::cout << "MyStruct:";
MyStruct s;
checkT(s);
std::cout << "MyUnion: ";
check<MyUnion>();
std::cout << "enum: ";
checkT(e);
std::cout << "myfunc():";
checkT(myfunc);
}
The program has the following output:
int: !IsClassT
MyClass: IsClassT
MyStruct: IsClassT
MyUnion: IsClassT
enum: !IsClassT
myfunc(): !IsClassT