我有一个std::unordered_multimap,我想获得特定键的最后插入元素.我观察到了这种行为:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_multimap<string, string> mmap;
mmap.emplace("a", "first");
mmap.emplace("a", "second");
mmap.emplace("a", "last");
mmap.emplace("b", "1");
mmap.emplace("b", "2");
mmap.emplace("b", "3");
auto last_a = mmap.equal_range("a").first;
auto last_b = mmap.equal_range("b").first;
cout << last_a->second << endl;
cout << last_b->second << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码输出:
last
3
Run Code Online (Sandbox Code Playgroud)
这至少是GCC,我想要的行为.我可以依靠吗?标准是否对std::unordered_multimap商店的订单说了什么?如果没有,最好的选择是什么?
我有这个代码:
struct Base {};
template<typename T>
struct Foo : Base {};
struct Bar {
template<typename T> // v--- What's happening here?
Bar(T foo) : baz{std::make_unique<Foo>(foo)} {}
std::unique_ptr<Base> baz;
};
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,GCC和Clang接受并编译了它.它似乎推断出模板参数Foo,但它没有意义.为什么编译器接受即使没有重载std::make_unique需要模板模板参数呢?Live example
我最近有这样的代码问题:
constexpr auto lambda = []{};
template<auto& l>
struct Lambda {};
template<auto& l>
void test(Lambda<l>) {}
int main() {
test(Lambda<lambda>{});
}
Run Code Online (Sandbox Code Playgroud)
clang和GCC都说不能推断l.
但是,如果我在那里添加const:
// ----v
template<const auto& l>
void test(Lambda<l>) {}
Run Code Online (Sandbox Code Playgroud)
然后一切都与clang一起工作.GCC仍然失败.这里发生了什么事?难道它不能推断const自己吗?这是否是GCC的错误,l在两种情况下都没有推断?
让我们采取两个结构/类
struct C1{
C1(){};
C1(C1&){std::cout<<"copy"<<std::endl;}
C1(C1&&){std::cout<<"move"<<std::endl;}};
struct C2{
C1 c;
C2(){};
C1 get1(){return c;}
C1 get2(){return std::move(c);}};
Run Code Online (Sandbox Code Playgroud)
然后
C1 a1=C2().c;
C1 a2=C2().get1();
C1 a3=C2().get2();
Run Code Online (Sandbox Code Playgroud)
输出是
move
copy
move
Run Code Online (Sandbox Code Playgroud)
我们知道rvalues的成员本身就是rvalues.这就是为什么使用a1,调用移动构造函数.为什么,在a2的情况下调用复制构造函数.我们从函数返回一个rvalue.
换句话说,std :: move强制转换为右值.但是,作为一个rvalue的成员,c,已经是一个rvalue.为什么a2和a3的行为之间存在差异?
我在尝试为C++17 中的自定义类专门化tuple_size/tuple_element以进行结构化绑定时遇到了这个问题。
下面的代码在 GCC 中编译,但不在 clang 中(两个主干版本,见下面的链接)。
#include <type_traits>
template<typename T, typename... Ts>
using sfinae_t = T;
template<typename T, bool... Bs>
using sfinae_v_t = sfinae_t<T, typename std::enable_if<Bs>::type...>;
template <typename T>
struct Test;
template <typename T>
struct Test<sfinae_v_t<T, std::is_integral_v<T>>> {};
void f() {
Test<int> t;
}
Run Code Online (Sandbox Code Playgroud)
这是 clang 提供的错误:
<source>:13:8: error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
struct Test<sfinae_v_t<T, std::is_integral<T>::value>> {};
^ …Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization template-specialization language-lawyer
我正在尝试将DI与我的消费者类一起使用而没有成功.
我的消费者阶层:
public class TakeMeasureConsumer : IConsumer<TakeMeasure>
{
private IUnitOfWorkAsync _uow;
private IInstrumentOutputDomainService _instrumentOutputDomainService;
public TakeMeasureConsumer(IUnitOfWorkAsync uow,
IInstrumentOutputDomainService instrumentOutputDomainService)
{
_uow = uow;
_instrumentOutputDomainService = instrumentOutputDomainService;
}
public async Task Consume(ConsumeContext<TakeMeasure> context)
{
var instrumentOutput = Mapper.Map<InstrumentOutput>(context.Message);
_instrumentOutputDomainService.Insert(instrumentOutput);
await _uow.SaveChangesAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
当我想注册总线工厂时,消费者必须有一个无参数构造函数.
protected override void Load(ContainerBuilder builder)
{
builder.Register(context =>
Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ReceiveEndpoint(host, "intrument_take_measure", e =>
{
// Must be a non abastract type with a parameterless …Run Code Online (Sandbox Code Playgroud) 我有以下问题:
template< std::size_t N >
class A
{
std::function< std::size_t( /*std::size_t,....,std::size_t <- N-times*/) > foo;
};
Run Code Online (Sandbox Code Playgroud)
正如您在上面所看到的,我尝试将a声明std::function<...> foo为类的成员A.在这里,我希望foo具有返回类型std::size_t(这没有问题)并且作为输入,我将传递N次类型std::size_t但我不知道如何.有可能吗?
提前谢谢了.
我想知道根据C++标准,以下是合法的:
struct Abstract { virtual ~Abstract() = 0; };
auto get_type() -> Abstract;
// I use `get_type` only to extract the return type.
using MyType = decltype(get_type());
Run Code Online (Sandbox Code Playgroud)
GCC 6.3接受了它,但Clang 3.9拒绝了它.
但是,如果我这样做:
auto get_type() -> struct Abstract;
struct Abstract { virtual ~Abstract() = 0; };
using MyType = decltype(get_type());
Run Code Online (Sandbox Code Playgroud)
现在两个编译器都接受它.在这种情况下他们都错了吗?
我有一些代码使用模板转换运算符来查找通过ADL找到的函数的返回类型.
简化的代码如下所示:
#include <type_traits>
template<typename S>
struct probe {
template<typename T, typename U = S, std::enable_if_t<
std::is_same<T&, U>::value &&
!std::is_const<T>::value, int> = 0>
operator T& ();
template<typename T, typename U = S&&, std::enable_if_t<
std::is_same<T&&, U>::value &&
!std::is_const<T>::value, int> = 0>
operator T&& ();
template<typename T, typename U = S, std::enable_if_t<
std::is_same<T const&, U>::value, int> = 0>
operator T const& () const;
template<typename T, typename U = S&&, std::enable_if_t<
std::is_same<T const&&, U>::value, int> = 0>
operator T const&& () const;
}; …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个模板化的 num 类。这个类需要有一个公共属性 ,val和 type T,这是唯一的模板化参数。此外,如果提供了一个值,则属性 ( val) 应使用该值进行初始化。为此,我编写了以下代码:
#include <iostream>
template<class T>
class Num {
public:
T val;
Num():val(0) { std::cout<<"default constr used"<<std::endl; }
Num(T value):val(value) {std::cout<<"constr (T value) used"<<std::endl; }
~Num() { std::cout<<"destructor used"<<std::endl; }
template<typename U>
Num operator+(const Num<U>& other) {
return val+other.value;
}
};
Run Code Online (Sandbox Code Playgroud)
此外,我创建了main()测试程序的函数,如下所示:
int main() {
std::cout << Num<int>(1) + Num<double>(2.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,程序的结果是现在3。而我期望它是3.0(类型double)。