Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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 - Fatal编程技术网

C++ 如何编写能够处理对象或指针函数调用的模板函数?

C++ 如何编写能够处理对象或指针函数调用的模板函数?,c++,templates,C++,Templates,我希望能够编写一个模板函数,可以调用容器中所有元素的函数调用。我们可以假设函数名总是相同的。然而,不知道的是容器是否包含对象或指针。例如,我是否应该取消引用 template< typename TContainer > void ProcessKeyedContainer( TContainer &keyedContainer ) { for ( auto it = keyedContainer.begin(); it != keyedContainer.end();

我希望能够编写一个模板函数,可以调用容器中所有元素的函数调用。我们可以假设函数名总是相同的。然而,不知道的是容器是否包含对象或指针。例如,我是否应该取消引用

template< typename TContainer >
void ProcessKeyedContainer( TContainer &keyedContainer )
{
  for ( auto it = keyedContainer.begin(); it != keyedContainer.end(); ++it )
  {
    // do some random stuff here.
    // ...

    auto value = it->second;
    value.Process(); // or value->Process() if the container has pointers
  }
}

...

std::map< int, CMyObject > containerOfObjects;
containerOfObjects[0] = CMyObject();

std::map< int, CMyObject* > containerOfPointers;
containerOfPointers[0] = new CMyObject();

// I would like both calls to look near identical
ProcessKeyedContainer( containerOfObjects ); 
ProcessKeyedContainer( containerOfPointers );
模板
void ProcessKeyedContainer(t容器和keyedContainer)
{
for(auto-it=keyedContainer.begin();it!=keyedContainer.end();++it)
{
//在这里做一些随机的事情。
// ...
自动值=它->秒;
value.Process();//或value->Process(),如果容器有指针
}
}
...
标准::映射containerOfObjects;
containerOfObjects[0]=CMyObject();
标准::映射容器指针;
containerOfPointers[0]=新的CMyObject();
//我希望两个电话看起来几乎相同
ProcessKeyedContainer(容器对象);
ProcessKeyedContainer(containerOfPointers);

有没有一种简洁的方法能够在ProcessKeyedContainer内进行流程调用,而不给调用者带来负担(即调用者不必知道如何以一种方式将其用于指针,另一种方式用于对象),并且不必复制太多代码?

重载函数模板是救星:

template<typename T>
void invoke(T * obj)  //when object is pointer
{
      obj->Process();
}

template<typename T>
void invoke(T & obj)  //when object is non-pointer
{
      obj.Process();
}
但这还不够好,因为您可能还想在自己编写的函数的其余部分中使用
value
执行其他操作。因此,如果遵循上述方法,就会出现代码重复,因为两个
invoke()
的代码几乎相似

因此,这里有一个改进:
invoke()
,而不是使用
invoke()
将指针转换为引用,以便在函数中统一使用它

template<typename T>
T& ensure_ref(T * obj)  //when object is pointer
{
      return *obj; //return the dereferenced object
}

template<typename T>
T& ensure_ref(T & obj)  //when object is non-pointer
{
      return obj; //simply return it
}

希望有帮助

+1,我将通过将迭代器作为
ProcessKeyedContainer()
的函数参数而不是容器引用来进一步完善它。谢谢!确保ref完全满足我的需要。
template<typename T>
T& ensure_ref(T * obj)  //when object is pointer
{
      return *obj; //return the dereferenced object
}

template<typename T>
T& ensure_ref(T & obj)  //when object is non-pointer
{
      return obj; //simply return it
}
auto & value = ensure_ref(it->second); //call ensure_ref to ensure reference!

value.Process(); //value is gauranteed to be NOT pointer!

//you might want to do this also!
value.xyz = abc;