小编alf*_*lfC的帖子

在C++中重命名(别名/转发)函数的最佳方法是什么?

(我将这个问题限制在C++ 11中,因为我相信在C++ 98中没有通用的方法).

假设我有一组复杂的(在签名方面)模板函数和/或重载函数,我想以完全相同的方式使用这些函数,但使用不同的名称(即别名).

例如:

template<class A, class B, class C> 
D fun(A a, B& b, C&& c){ ... }

template<class E, class F> 
G fun(H<E> he, F& f){ ... }

... many other versions of fun
Run Code Online (Sandbox Code Playgroud)

现在假设我想要同时重命名(或别名,或转发为更精确)这些函数(即能够为同一函数使用不同的名称而不重写它).这样,在代码的其他部分,我可以使用不同的名称,而无需修改上述代码.

这是正确的方式重命名(别名/转发)fungun

template<typename... Args> 
inline auto gun(Args&&... args)->decltype(fun(std::forward<Args>(args)...)){
    return fun(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
  • 它真的一般吗?
  • 这是最简单的方法吗?
  • 这是最佳方式吗?(例如可以内联,没有不必要的副本)
  • 如果原始功能有一些SFINAE功能怎么办?(例如template<class A, class B, class C, class = std::enable_if< ... >::type>),是否会decltype在所有情况下转移SFINAE? …

c++ templates variadic-templates perfect-forwarding c++11

20
推荐指数
1
解决办法
1810
查看次数

如何编写具有高过载优先级的类似标准的函数

在通用函数中,我使用以下习语,

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    ... other stuff here...
    using std::copy;
    copy(first, second, d_first);
}
Run Code Online (Sandbox Code Playgroud)

do_something是一个通用函数,不应该知道任何其他任何库(可能除外std::).

现在假设我的命名空间中有几个迭代器N.

namespace N{

  struct itA{using trait = void;};
  struct itB{using trait = void;};
  struct itC{using trait = void;};

}
Run Code Online (Sandbox Code Playgroud)

我想在这个命名空间中重载这些迭代器的副本.我当然会这样做:

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,当我打电话do_somethingN::A,N::BN::C争论,我得到"暧昧通话复制"即使这些都在同一个命名空间N::copy.

有没有办法 …

c++ ambiguous argument-dependent-lookup c++11

17
推荐指数
1
解决办法
863
查看次数

如何在现代C++中使用allocator

从我在http://en.cppreference.com/w/cpp/memory/allocator中读到的内容来看,分配器的大部分功能现在都将被弃用.问题是,如何在新代码中使用分配器?现在"正确"的方式是什么?

从我在文档中推断出来的,construct是分配器特性的一部分,而不是分配器本身.

我正在构建一个自定义容器,这里是一个非常简单的构造函数版本,这是一个很好的新设计用法吗?

container::container(std::size_t size, T const& value, Allocator const& allocator) : allocator_(allocator){
    data_ = std::allocator_traits<Alloc>::allocate(allocator_, size);
    for(auto ptr = data_; ptr != data_ + size; ++ptr){
        std::allocator_traits<Allocator>::construct(allocator_, ptr, value)
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图std::for_each在循环中使用一个算法(如),但我没有设法使用一个没有地址(operator&).

我在哪里可以找到现代分配器的完整示例?


经过一些调整,我找到了一种方法来使用算法而不是原始循环(可以传递执行策略).我不是很确定,但可能是这样的:

    data_ = std::allocator_traits<Allocator>::allocate(allocator_, size);
    std::for_each([policy? deduced from allocator?,] 
        boost::make_counting_iterator(data_),
        boost::make_counting_iterator(data_ + size), 
        [&](auto ptr){std::allocator_traits<Allocator>::construct(allocator_, ptr, value);}
    );
Run Code Online (Sandbox Code Playgroud)

c++ allocator type-traits c++11 c++17

16
推荐指数
1
解决办法
1801
查看次数

boost :: pool <> :: malloc和boost :: pool <> :: ordered_malloc之间有什么区别,什么时候应该使用boost :: pool <> :: ordered_malloc?

我正在使用boost.pool,但我不知道何时使用boost::pool<>::mallocboost::pool<>::ordered_malloc

所以,

  1. 什么是boost::pool<>::mallocboost::pool<>::ordered_malloc?的区别?

  2. 我应该什么时候使用boost::pool<>::ordered_malloc

boost pool

15
推荐指数
1
解决办法
2056
查看次数

C++ 向量成员初始化

vec我对以下程序中有关in的输出感到困惑Test。为什么它是一个大小为 100 而不是 1 的向量?我以为std::vector<T> var{a}是一样的std::vector<T> var = {a}

#include <iostream>
#include <vector>

using namespace std;
struct Value {
   int a;
   int b;
};

class Test {
  public:
    std::vector<struct Value> vec{100};  
};

class Test2 {
  public:
    std::vector<int> vec{100};  
};

int main()
{
    Test test;
    std::cout << "test size: " << test.vec.size() << std::endl;
    Test2 test2;
    std::cout << "test2 size: " << test2.vec.size();
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

test size: 100
test2 size: 1
Run Code Online (Sandbox Code Playgroud)

c++

13
推荐指数
1
解决办法
1120
查看次数

MPI_Isend请求参数

使用时MPI_Isend,MPI_Request参数可以是空指针(当发送者发送后不关心消息时)?

c mpi

12
推荐指数
1
解决办法
1万
查看次数

如何在Boost.Process 0.5中将程序终止与流结束绑定?

在Boost.Process 0.5(http://www.highscore.de/boost/process0.5/index.html)的这个简单示例中,程序(ls)的输出正在输入流.流工作正常但与预期相反,流程在程序完成后不会变为无效(例如,流末尾)(类似于之前版本的Boost.Process,例如http://www.highscore.de/boost /process/index.html)

is在子程序退出后,为了使流(在示例中)自动无效,我缺少什么?

也许我必须在Boost.Streams stream中设置一个选项file_descriptor

#include <boost/process.hpp> // version 0.5 from http://www.highscore.de/boost/process0.5/process.zip
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;
int main(){
    boost::process::pipe p = create_pipe();
    file_descriptor_sink sink(p.sink, close_handle);
    child c = execute(run_exe("/usr/bin/ls"), bind_stdout(sink));
    file_descriptor_source source(p.source,  close_handle);
    stream<file_descriptor_source> is(source);
    std::string s;
    while(std::getline(is, s)){
        std::cout << "read: " << s << std::endl;
    }
    std::clog << "end" << std::endl; // never reach
}
Run Code Online (Sandbox Code Playgroud)

c++ boost process boost-iostreams

11
推荐指数
1
解决办法
1561
查看次数

boost :: range_iterator和boost :: iterator_range混乱

我一直在浏览boost::range图书馆并注意到boost :: range_iterator和boost::iterator_range.我在这里对这些术语感到困惑.请问有谁可以解释两者之间有什么区别以及什么时候使用什么?此外,如果您能指出示例示例,除了文档之外,还可以使用增强范围库来了解更多相关信息.谢谢

c++ boost boost-range

11
推荐指数
1
解决办法
9658
查看次数

构造函数干扰成员变量指定的初始化程序?

有一段时间了,人们已经能够在GCC中使用"指定的初始化程序":

struct CC{
    double a_;
    double b_;
};

CC cc{.a_ = 1., .b_ = 2.}; assert(cc.a_ == 1. and cc.b_ == 2.); // ok
CC cc{.bla = 0., .bli = 0.}; // compile error
Run Code Online (Sandbox Code Playgroud)

但是,当我添加构造函数时,标签将被忽略.

struct CC{
    double a_;
    double b_;
    CC(double a, double b) : a_{a}, b_{b}{}
};

CC cc{.a_ = 1., .b_ = 2.}; assert(cc.a_ == 1. and cc.b_ == 2.); // ok
CC cc{.b_ = 2., .a_ = 1.}; // compiles but labels don't matter only the …
Run Code Online (Sandbox Code Playgroud)

c++ g++ designated-initializer language-lawyer c++20

11
推荐指数
2
解决办法
405
查看次数

什么是具有 ID 相关向后分支的循环?

我试图理解这个铿锵有力的警告:altera-id-dependent-backward-branch这似乎是由这个循环触发的。

        for(; first != current; ++first)
Run Code Online (Sandbox Code Playgroud)

我的例子是这段代码,它看起来几乎完全是std::uninitialized_fill_n.

静态分析器抱怨说:

error: backward branch (for loop) is ID-dependent due to variable reference to 'current' and may cause performance degradation [altera-id-dependent-backward-branch,-warnings-as-errors]
                        for(; current != first; ++first) {
Run Code Online (Sandbox Code Playgroud)
uninitialized_fill_n(ForwardIt first, Size n, T const& v) {
    ForwardIt current = first;
    try {
        for(; n > 0; ++current, --n) {
            new (std::addressof(*current)) T(v);
        }
        return current;
    } catch(...) {
        for(; first != current; ++first) {  // clang-tidy error here
            std::addressof(*first)->~T();
        }
        throw; …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop clang-tidy

11
推荐指数
1
解决办法
1302
查看次数