相关疑难解决方法(0)

如何检测类中是否有特定的成员变量?

为了创建算法模板函数,我需要知道类中的x或X(和y或Y)是模板参数.当我的函数用于MFC CPoint类或GDI + PointF类或其他类时,它可能很有用.他们都使用不同的x.我的解决方案可以简化为以下代码:


template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(&P::x)>::type b = 0) { return true; }
template<class P> bool Check_x(P p, typename TT<sizeof(&P::X)>::type b = 0) { return false; }

struct P1 {int x; };
struct P2 {float X; };
// it also could be struct P3 {unknown_type X; };

int main()
{
    P1 p1 = {1};
    P2 p2 = {1};

    Check_x(p1); // must return true
    Check_x(p2); // must return false

    return …
Run Code Online (Sandbox Code Playgroud)

c++ templates g++ sfinae visual-studio

62
推荐指数
5
解决办法
3万
查看次数

为什么使用两个sizeofs来检查一个类是否是默认可构造的,但是一个不是?

我使用了" 有没有办法测试C++类是否有默认构造函数(编译器提供的类型特征除外)? "的代码.

我稍微修改它以适应我的所有测试用例:

template< class T >
class is_default_constructible {
    typedef int yes;
    typedef char no;


    // the second version does not work
#if 1
    template<int x, int y> class is_equal {};
    template<int x> class is_equal<x,x> { typedef void type; };

    template< class U >
    static yes sfinae( typename is_equal< sizeof U(), sizeof U() >::type * );
#else
    template<int x> class is_okay { typedef void type; };

    template< class U >
    static yes sfinae( typename is_okay< sizeof U() >::type …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming

19
推荐指数
1
解决办法
1005
查看次数

如何检查类是否具有默认构造函数(public,protected或private)

我需要检查一个类C是否有一个默认的构造函数,无论是隐式的还是自定义的,或者是public,protected或者private.

我尝试使用std::is_default_constructible<C>::value,如果返回真C有一个public默认的构造函数(隐式或自定义),但false如果C有一个protectedprivate默认的构造函数(接缝是标准的行为虽然).

有没有办法检查一个类是否有一个protectedprivate默认的构造函数?

注意(如果这可能有帮助):检查是从要检查friend的类的函数执行的C.


我需要执行此检查,以便默认构造对应于元组nullptr指针的对象,对象的m_objs成员Foo(下面的部分Foo定义):

template<class... Objects>
class Foo
{
public:
    Foo(Objects*... objects)
    : m_objs(objects...)
    {
        // User construct a Foo objects passing a pack of pointers
        // some of them are nullptr, some are not.
        // The following call should default-construct objects corresponding …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

8
推荐指数
1
解决办法
2016
查看次数

有没有办法测试默认构造函数不存在?

我正在编写一个类型的测试驱动程序,这显然不应该是默认构造的.有没有办法在我的测试驱动程序中声明这是这种情况?我可以通过编译错误手动验证,但我想要一些可以防止未来可能错误地添加默认构造函数的更改.

编辑:我陷入了使用C++ 03的环境.记住这一点,还有其他选择is_default_constructable吗?

c++ testing c++03

6
推荐指数
1
解决办法
160
查看次数

如何检查模板参数是否是默认可构造的

我正在编写一个模板类,并想知道模板参数是否是默认的可构造的有没有办法做到这一点?

代码类似于以下内容

template <class C>
class A
{

createObj()
{
C* objPtr = NULL;
// If default constructible then create object else let it remain NULL
}
};
Run Code Online (Sandbox Code Playgroud)

更新:我已尝试使用此问题中给出的代码,但它不起作用,确切地说,如果返回默认可构造甚至对于那些不是的类,我不知道为什么会发生这种情况.

c++ implementation templates

5
推荐指数
1
解决办法
2181
查看次数

检查类是否具有TMP的复制构造函数

我一直在尝试一点SFINAE来确定泛型类型T是否有我可以使用的复制构造函数.这是我现在的位置.

template <bool statement, typename out>
struct Failable
{
    typedef out Type;
};
//This class is only used to insert statements that 
//could encounter substitution failure


template <typename O>
struct COPY
{
    template <typename T>
    typename Failable<true == sizeof(&T::T(const T&)), char>::Type copy(int)
    {}

    template <typename T>
    typename Failable<true, int>::Type copy(...)
    {}

};
Run Code Online (Sandbox Code Playgroud)

然而,这也是我有点卡住的地方. &T::T(const T&)显然是一个无效的语句,因为我们不能提供带有指向成员的参数列表,即使是ptm函数也是如此.我总是可以尝试指定某种类型void (T::*ptmf)(const T&) = &T::T,并希望它隐式确定正确的重载构造函数放入指向成员函数的指针,但这也意味着构造函数具有特定的返回类型,我必须指定.

有没有其他人有任何我可以跋涉的想法?(我还需要应用类似的概念来检查赋值运算符.)

提前致谢.

c++ sfinae pointer-to-member template-meta-programming

2
推荐指数
1
解决办法
1567
查看次数

检测编译时是否存在默认构造函数

我正在尝试检查模板参数是否存在默认构造函数.我想做这样的事情:

template <typename A>
class Blah
{
   Blah() { A* = new A(); } 
}
Run Code Online (Sandbox Code Playgroud)

但是我希望在编译时通过SFINAE或其他技巧检测,如果该构造函数存在,并且static_assert如果不存在则引发我自己的.

当我有类(没有std::vector)没有"默认构造函数"但是具有默认参数的构造函数时,问题就出现了.

所以使用std::has_trivial_default_constructor不会返回true.虽然我可以使用new vector<T>().

c++ templates constructor sfinae

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