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 - Fatal编程技术网

C++ 将模板参数类型转换为字符串

C++ 将模板参数类型转换为字符串,c++,templates,C++,Templates,我和其他人编写的代码中有一点枯燥,我想减少,但我没有弄清楚如何完成。这是遗留的COM代码,但会影响可读性。我想做以下几点: bool queryInterface<class T, class V>(T &_input, V &_output, Logger &_logger){ if( FAILED( _input->QueryInterface( &_output ) ) ){ _logger.error() <

我和其他人编写的代码中有一点枯燥,我想减少,但我没有弄清楚如何完成。这是遗留的COM代码,但会影响可读性。我想做以下几点:

bool queryInterface<class T, class V>(T &_input, V &_output, Logger &_logger){
    if( FAILED( _input->QueryInterface( &_output ) ) ){
        _logger.error() << "Failed to Query Interface between " << MAGICHAPPENS<T>() 
                        << " and " << MAGICHAPPENS<V>();

        return false;
    }
    if( _output == NULL ){
        _logger.warn() << "Unable to Query Interface between " << MAGICHAPPENS<T>()
                       << " and " << MAGICHAPPENS<V>();

        return false;
    }
}
bool查询接口(T&u输入、V&u输出、记录器&u记录器){
if(失败(_输入->查询接口(&_输出))){

_logger.error()您可以使用RTTI获取变量名:

#include <typeinfo>

template <typename T>
const char* type_name(void)
{
    // this, unfortunately, is implementation defined
    // and is allowed to be an empty string (useless!)
    return typeid(T).name(); 
}

_logger.error() << "Failed to Query Interface between " << type_name<T>() 
                    << " and " << type_name<V>();
#包括
模板
常量字符*类型名称(无效)
{
//不幸的是,这是实现定义的
//并且允许为空字符串(无用!)
返回typeid(T.name();
}

_logger.error()顺便说一下,您可以这样做:
如果(失败(\u input->QueryInterface(&\u output))\124;\ u output==NULL)
不重复条件代码。另外,如果
\u input
\u output
应该是指针,您应该指出:
T*\u input,V*&u output
(是否
\u input
需要作为参考?)最后,您缺少了一个
返回true;
:)您不使用ATL有什么特别的原因吗?它已经提供了安全QI和跟踪功能。它们是智能指针对象,键入按引用传递是习惯。我们最好单独执行
==NULL
,因为它可以让我们在调试时知道是否有错误他们正在使用的库的任何版本都有问题,或者这是一个实际的失败。但是,再次感谢。我非常感谢。@gf Dunno。你说的是ATL的哪一部分?这些对象是_com_prt_t<\u ComiID>对象(技术上是第三方API)我很想了解任何有助于解决问题的东西。例如,当一致使用ATL时,
\define\u ATL\u DEBUG\u QI
已经提供了QI跟踪。我还发现
CComQIPtr
非常宝贵。嗯,尽管是第三方-我不记得,
CComPtr
等是否以有用的格式跟踪了它。谢谢您,先生。我会的试试这个!