C++ 如何在C++;获取对象成员的步骤

C++ 如何在C++;获取对象成员的步骤,c++,templates,constructor,c++14,sfinae,C++,Templates,Constructor,C++14,Sfinae,我有一个模板类eglue,它根据传入的一个参数确定自己的n_行和n_列,保证有n_行和n_列成员。代码如下: template<typename> struct isEglueOrMat : std::false_type {}; template<typename T1, operations _op, typename T2> struct isEglueOrMat<eglue<T1, _op, T2>> : std::true_type {}

我有一个模板类
eglue
,它根据传入的一个参数确定自己的n_行和n_列,保证有n_行和n_列成员。代码如下:

template<typename> struct isEglueOrMat : std::false_type {};
template<typename T1, operations _op, typename T2> struct isEglueOrMat<eglue<T1, _op, T2>> : std::true_type {};
template<> struct isEglueOrMat<Matrix> : std::true_type {};

template<typename T1, operations _op, typename T2>
class eglue {
public:
    const T1& First;
    const T2& Second;
    const unsigned n_rows;
    const unsigned n_cols;
    eglue(const T1& f, const T2& s) : First(f), Second(s), n_rows(isEglueOrMat<T1>()? f.n_rows:s.n_rows), n_cols(isEglueOrMat<T1>()? f.n_cols:s.n_cols) {}
这会出错

type/value mismatch at argument 1 in template parameter list for 'template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type'
     template<typename Dummy = void, typename Dummy2 = std::enable_if_t<isEglueOrMat<T1>(), Dummy>> eglue(const T1& f, const T2& s) : First(f), Second(s), n_rows(f.n_rows), n_cols(f.n_cols) {}
                                                                                            ^
error:   expected a constant of type 'bool', got 'isEglueOrMat<T1>()'
“模板使用enable\u if\u t=typename std::enable\u if::type”的模板参数列表中参数1的类型/值不匹配 模板eglue(常数T1&f,常数T2&s):第一(f),第二(s),n_行(f.n_行),n_列(f.n_列){} ^ 错误:应为“bool”类型的常量,但得到了“isEglueOrMat()” 尽管我已经在代码的其他地方成功地使用了
isEglueOrMat()
(如果必要的话,我会发布这些,但我已经看到它在其他情况下也能工作)

我知道这可能是一个重复或类似的其他问题,但我真的没有能够得到这个工作。。。任何建议都将不胜感激

编辑:我是个白痴,第二种方法无论如何都不起作用,因为你不能用相同的签名重载构造函数。。。。
有什么方法可以使第一种方法(或任何其他方法!)起作用吗?

对于第二种方法,您应该使用
isEglueOrMat::value
作为布尔常数。当您使用
isEglueOrMat()
时,它被视为一个类型为
integral\u constant
的对象,一种结构。当您在其他布尔表达式中使用它时,它被静默地强制为bool(
std::integral_constant
提供了这样的运算符)。另一个选择是显式地将其强制转换为bool,
static_cast(isEglueOrMat())
和C-style cast一样工作


第一种方法也是可行的,但它需要更多的间接性(普通的标准操作符不会像你看到的那样工作)和一些乍看起来很棘手的SFINAE游戏。

你可以使用标记分派和委托构造函数:

template<typename T1, operations _op, typename T2>
class eglue {
public:
    const T1& First;
    const T2& Second;
    const unsigned n_rows;
    const unsigned n_cols;
    eglue(const T1& f, const T2& s) : eglue(f,s,isEglueOrMat<T1>()) {}
private:
    eglue(const T1& f, const T2& s, std::true_type)  : First(f), Second(s), n_rows(f.n_rows), n_cols(f.n_cols) {}
    eglue(const T1& f, const T2& s, std::false_type) : First(f), Second(s), n_rows(s.n_rows), n_cols(s.n_cols) {}
};

isEglueOrMat()
constexpr
?否则它不能用作模板参数。我在
template std::enable_if_t operator+(const T1&first,const T2&second){return eglue(first,second);}
中使用过它,它编译得非常好。。。即使它不是constexpr,我也不明白为什么它在这里工作而在那里不工作?否则我看不出如何使用这个表达式。是的,我用
std::is_integral
std::is_floating_point
定义了它。。。再说一遍,这不是问题所在/你到底想实例化什么<代码>eglue?不清楚T1/
T2
的可能“类型”是什么……我认为您不能声明structs
constexpr
?还是我遗漏了什么?哦,这是一个结构,对不起,我遗漏了。编辑帖子。你的回答没有意义“当你在其他布尔表达式中使用它时,它被默默地强制为布尔值。”,这正是你想要的。。。
template<typename T1, operations _op, typename T2>
class eglue {
public:
    const T1& First;
    const T2& Second;
    const unsigned n_rows;
    const unsigned n_cols;
    eglue(const T1& f, const T2& s) : eglue(f,s,isEglueOrMat<T1>()) {}
private:
    eglue(const T1& f, const T2& s, std::true_type)  : First(f), Second(s), n_rows(f.n_rows), n_cols(f.n_cols) {}
    eglue(const T1& f, const T2& s, std::false_type) : First(f), Second(s), n_rows(s.n_rows), n_cols(s.n_cols) {}
};
template<typename T1, operations _op, typename T2>
class eglue {
public:
    const T1& First;
    const T2& Second;
    const unsigned n_rows;
    const unsigned n_cols;
    template <typename _T1 = T1, std::enable_if_t<isEglueOrMat<_T1>{}, bool> = true>
    eglue(const T1& f, const T2& s)  : First(f), Second(s), n_rows(f.n_rows), n_cols(f.n_cols) {}
    template <typename _T1 = T1, std::enable_if_t<!isEglueOrMat<_T1>{}, bool> = false>
    eglue(const T1& f, const T2& s)  : First(f), Second(s), n_rows(s.n_rows), n_cols(s.n_cols) {}
};