Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:在模板化类的模板化类型中存在命名成员时,是否在模板化类中提供类函数?_C++_Sfinae_Typetraits_Enable If_Class Template - Fatal编程技术网

C++ C++;:在模板化类的模板化类型中存在命名成员时,是否在模板化类中提供类函数?

C++ C++;:在模板化类的模板化类型中存在命名成员时,是否在模板化类中提供类函数?,c++,sfinae,typetraits,enable-if,class-template,C++,Sfinae,Typetraits,Enable If,Class Template,我正在尝试执行以下操作:模板化类应该提供一些函数,这些函数取决于模板化的类型是否包含具有给定名称的成员变量。例如,以下伪代码应仅在模板化结构/类具有名为“id”的成员时提供“printid()”: #包括 #包括 结构A{int id;}; 结构B{}; 模板 福班 { T-myvar; 公众: #if exists T.id(或备选方案:#if exists myvar.id) printid(){std::cout是的,这是可能的。下面是一个示例: template<typename

我正在尝试执行以下操作:模板化类应该提供一些函数,这些函数取决于模板化的类型是否包含具有给定名称的成员变量。例如,以下伪代码应仅在模板化结构/类具有名为“id”的成员时提供“printid()”:

#包括
#包括
结构A{int id;};
结构B{};
模板
福班
{
T-myvar;
公众:
#if exists T.id(或备选方案:#if exists myvar.id)

printid(){std::cout是的,这是可能的。下面是一个示例:

template<typename T>
class foo
{
  T myvar;

public:
  template <class _T = T,
            class = typename std::enable_if<
                      !std::is_function<decltype(_T::id)>::value>
                    ::type>
  void printid() { std::cout << "I have element id."; }
};
模板
福班
{
T-myvar;
公众:
模板
::类型>

void printid(){std::cout按预期工作。我永远不会知道如何声明此巫毒。将其包装在定义中以保存多个函数的键入现在是小菜一碟。非常感谢。
template<typename T, typename = void>
struct has_id : std::false_type { };

template<typename T>
struct has_id<T, decltype(std::declval<T>().id, void())> : std::true_type { };
template<typename T>
class foo
{
  T myvar;

public:
  template <class _T = T,
            class = typename std::enable_if<
                      !std::is_function<decltype(_T::id)>::value>
                    ::type>
  void printid() { std::cout << "I have element id."; }
};