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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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++;模板还是类似的?_C++_Templates_Insert_Macros_Code Duplication - Fatal编程技术网

C++ 如何转换经常使用的大型C++;每次使用都会将微小的宏更改为C++;模板还是类似的?

C++ 如何转换经常使用的大型C++;每次使用都会将微小的宏更改为C++;模板还是类似的?,c++,templates,insert,macros,code-duplication,C++,Templates,Insert,Macros,Code Duplication,我正在处理一个容器,其代码类似于以下伪代码: #define LARGE_INSERT_MACRO (ASSIGNMENT_OBJECT) \ if (some_stuff) \ { \ ... do more stuff ... \ allocator.construct(place_to_insert_to, ASSIGNMENT_OBJECT); \ ... etc ... \ } \ else if (other_stuff) \ { \ ... do d

我正在处理一个容器,其代码类似于以下伪代码:

#define LARGE_INSERT_MACRO (ASSIGNMENT_OBJECT) \
if (some_stuff) \
{ \
    ... do more stuff ... \
    allocator.construct(place_to_insert_to, ASSIGNMENT_OBJECT); \
    ... etc ... \
} \
else if (other_stuff) \
{ \
    ... do different stuff ...  \
    allocator.construct(place_to_insert_to, ASSIGNMENT_OBJECT); \
    ... etc ... \
} \
else \
{ \
    ... do other different stuff ... \
    allocator.construct(place_to_insert_to, ASSIGNMENT_OBJECT); \
    ... etc ... \
} \
... more stuff again... \


iterator insert(the_type &object)
{
    LARGE_INSERT_MACRO(object)
}


iterator insert(the_type &&object)
{
    LARGE_INSERT_MACRO(std::move(object))
}


template<typename... Arguments> iterator emplace(Arguments... parameters)
{
    LARGE_INSERT_MACRO(parameters...)
}
#定义大的插入宏(赋值对象)\
如果(一些东西)\
{ \
…做更多的事情\
构造(place\u to\u insert\u to,ASSIGNMENT\u OBJECT)\
……等等\
} \
else if(其他内容)\
{ \
…做不同的事情\
构造(place\u to\u insert\u to,ASSIGNMENT\u OBJECT)\
……等等\
} \
否则\
{ \
…做其他不同的事情\
构造(place\u to\u insert\u to,ASSIGNMENT\u OBJECT)\
……等等\
} \
... 又来了更多的东西\
迭代器插入(类型和对象)
{
大插入宏(对象)
}
迭代器插入(类型&&object)
{
大插入宏(标准::移动(对象))
}
模板迭代器模板(参数…参数)
{
大插入宏(参数…)
}
鉴于每个插入类型之前和之后的代码都是插入所必需的,在插入类型之间不会更改,并且不能分解为两个单独的“之前和之后”函数,如何更改这段(伪)代码,使其不再使用宏


一如既往,请继续讨论将宏更改为其他代码形式的主题。

答案是完全转储大的插入宏,并允许通用引用演绎的奇迹为您完成所有工作:

template<class TheType>
iterator insert(TheType&& object)  // object is deduced to be either r-value
                               // or l-value reference as required by context
{
    if(some_stuff) {
        ...
        return allocator.construct(place_to_insert_to, std::forward<TheType>(object));
    } else {
        ... and so on
    }
}
模板
迭代器插入(type&&object)//对象被推断为r值或
//或上下文要求的l值引用
{
如果(一些东西){
...
返回allocator.construct(place_to_insert_to,std::forward(object));
}否则{
等等
}
}
只有当调用站点是知道其包含类型的容器的成员函数时,才可能在调用站点使用不带模板参数的emplace。否则,您必须指定它。再次,请注意使用通用引用推断来完美地转发参数

template<class TheType, typename... Arguments> 
iterator emplace(Arguments&&... parameters)
{
    return insert(TheType(std::forward<Arguments>(parameters)...));
}
模板
迭代器模板(参数&&…参数)
{
返回insert(类型(std::forward(参数)…);
}

答案是完全转储大的insert宏,让通用参考推断奇迹为您完成所有工作:

template<class TheType>
iterator insert(TheType&& object)  // object is deduced to be either r-value
                               // or l-value reference as required by context
{
    if(some_stuff) {
        ...
        return allocator.construct(place_to_insert_to, std::forward<TheType>(object));
    } else {
        ... and so on
    }
}
模板
迭代器插入(type&&object)//对象被推断为r值或
//或上下文要求的l值引用
{
如果(一些东西){
...
返回allocator.construct(place_to_insert_to,std::forward(object));
}否则{
等等
}
}
只有当调用站点是知道其包含类型的容器的成员函数时,才可能在调用站点使用不带模板参数的emplace。否则,您必须指定它。再次,请注意使用通用引用推断来完美地转发参数

template<class TheType, typename... Arguments> 
iterator emplace(Arguments&&... parameters)
{
    return insert(TheType(std::forward<Arguments>(parameters)...));
}
模板
迭代器模板(参数&&…参数)
{
返回insert(类型(std::forward(参数)…);
}

答案是完全转储大的insert宏,让通用参考推断奇迹为您完成所有工作:

template<class TheType>
iterator insert(TheType&& object)  // object is deduced to be either r-value
                               // or l-value reference as required by context
{
    if(some_stuff) {
        ...
        return allocator.construct(place_to_insert_to, std::forward<TheType>(object));
    } else {
        ... and so on
    }
}
模板
迭代器插入(type&&object)//对象被推断为r值或
//或上下文要求的l值引用
{
如果(一些东西){
...
返回allocator.construct(place_to_insert_to,std::forward(object));
}否则{
等等
}
}
只有当调用站点是知道其包含类型的容器的成员函数时,才可能在调用站点使用不带模板参数的emplace。否则,您必须指定它。再次,请注意使用通用引用推断来完美地转发参数

template<class TheType, typename... Arguments> 
iterator emplace(Arguments&&... parameters)
{
    return insert(TheType(std::forward<Arguments>(parameters)...));
}
模板
迭代器模板(参数&&…参数)
{
返回insert(类型(std::forward(参数)…);
}

答案是完全转储大的insert宏,让通用参考推断奇迹为您完成所有工作:

template<class TheType>
iterator insert(TheType&& object)  // object is deduced to be either r-value
                               // or l-value reference as required by context
{
    if(some_stuff) {
        ...
        return allocator.construct(place_to_insert_to, std::forward<TheType>(object));
    } else {
        ... and so on
    }
}
模板
迭代器插入(type&&object)//对象被推断为r值或
//或上下文要求的l值引用
{
如果(一些东西){
...
返回allocator.construct(place_to_insert_to,std::forward(object));
}否则{
等等
}
}
只有当调用站点是知道其包含类型的容器的成员函数时,才可能在调用站点使用不带模板参数的emplace。否则,您必须指定它。再次,请注意使用通用引用推断来完美地转发参数

template<class TheType, typename... Arguments> 
iterator emplace(Arguments&&... parameters)
{
    return insert(TheType(std::forward<Arguments>(parameters)...));
}
模板
迭代器模板(参数&&…参数)
{
返回insert(类型(std::forward(参数)…);
}

很难理解为什么您的宏首先是一个宏。从您发布的内容来看,您应该可以用(模板化)函数替换
LARGE\u INSERT\u MACRO
。您尝试了什么,出现了什么问题?更好的是,一个模板版本的
insert()
,它将对象推断为通用引用。然后只有一个代码路径需要维护,很难理解为什么你的宏首先是一个宏。从您发布的内容来看,您应该可以用(模板化)函数替换
LARGE\u INSERT\u MACRO
。您尝试了什么,出现了什么问题?更好的是,一个模板版本的
insert()
,它将对象推断为通用引用。然后只有一个代码路径需要维护,很难理解为什么你的宏首先是一个宏。从您发布的内容来看,您应该可以用(模板化)函数替换
LARGE\u INSERT\u MACRO
。你尝试过什么,有什么问题吗?更好的是,一个模板化的虚拟现实