Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_C++11_Variadic Templates - Fatal编程技术网

C++ 在类中强制可变模板成员函数实例化

C++ 在类中强制可变模板成员函数实例化,c++,templates,c++11,variadic-templates,C++,Templates,C++11,Variadic Templates,我有一个具有可变成员函数的类: class class_name { template<ArgTypes.. args> some_return_type memberMethod(ArgTypes... args) { //stuff... } } 我猜这个错误是因为成员函数是一个可变模板 有没有办法在类定义块内强制可变模板成员函数实例化?(从我的注释到OP重复) 行instate force=&Self::memberMethod中的实际问题 实际上,这里不

我有一个具有可变成员函数的类:

class class_name {
  template<ArgTypes.. args>
  some_return_type memberMethod(ArgTypes... args) {
    //stuff...
  }
}
我猜这个错误是因为成员函数是一个可变模板

有没有办法在类定义块内强制可变模板成员函数实例化?

(从我的注释到OP重复)

instate force=&Self::memberMethod中的实际问题

实际上,这里不需要显式模板参数[over.over]/1:

在某些上下文中,不带参数的重载函数名的使用解析为[…]重载集中特定函数的成员函数指针。函数模板名称被认为是在这样的上下文中命名一组重载函数。所选函数的类型与上下文中所需目标类型的函数类型相同

i、 例如,由于
instate
定义了函数类型,因此编译器能够确定为名称
Self::memberMethod
选择哪个重载(此处:模板专门化)


可能有更简单的解决方案来强制实例化函数模板,即使在类定义中也是如此。我想到的一个方法是使用私有的
typedef
使用dummy=integral_常量(或
静态constepr instate dummy=&Self::memberMethod;


我很清楚,但不是100%确定
typedef
强制成员函数模板的实例化。当需要该函数的定义时,将实例化该函数模板,ODR建议这里就是这种情况:“如果一个函数的名称显示为一个可能的求值表达式,则使用ODR,如果它是唯一的查找结果或一组重载函数的选定成员”,以及“每个程序应包含该程序中使用的每个非内联函数或变量的一个定义“

您可以使用
auto
而不是
instate
auto-force=&Self::memberMethod。这样,您就不用编写使用
定义类型的
?@Nawaz很好。我仍然没有养成c++11的习惯。问题仍然悬而未决。。。。我认为您缺少了一个
模板
&Self::template memberMethod
@Marcin解释为什么
instate force=&Self::memberMethod工作原理是编译器可以根据该上下文中所需的类型(类型为
instate
)选择重载。当然,对于
decltype
,这是不可能的。因此,在decltype中,您需要强制转换或显式指定重载(例如,使用显式模板参数)。这当然是无用的,因为如果可以访问这些参数类型,可以直接指定函数类型w/o
decltype
template<typename Self, typename RetType, typename... ArgTypes>
struct force_instantation_imlp<Self, RetType, type_placeholder, type_placeholder<ArgTypes...>> {
    force_instantation_imlp() {
        using instate = RetType (Self::*)(ArgTypes...);
        instate force = &Self::memberMethod<ArgTypes...>;        
    }
};


class class_name {
  template<ArgTypes.. args>
  some_return_type memberMethod(ArgTypes... args) {
    //stuff...
  }

  force_instantation_imlp<class_name, some_return_type, rest_of_types_deduced_from_context> force_virtual_instantation;
}
error: expected primary-expression before ‘...’ token instate force = &Self::memberMethod<ArgTypes...>;
instate force = &Self::template memberMethod<ArgTypes...>;