小编dlf*_*dlf的帖子

为什么"extern模板"不能与shared_ptr一起使用?

为了防止在数百个文件中进行冗余实例化,我有了(似乎)明确extern template class std::shared_ptr<SomeWidelyUsedClass>在stdafx.h 中使用的好主意,想象我可以放在单个.cpp中以强制单个实例化并希望保存在编译中/链接时间.但是,检查生成的.cod和.obj文件表明无论如何都会在任何地方创建代码.但是,如果我使用与我自己的模板类完全相同的技术,它会按预期工作.有什么特别之处可以排除这种用途吗?也许某些东西本身会迫使编译器在它到达我的语句之前创建一个实例化(我很确定在stdafx.h中没有更高的东西可以使用)?#include <memory>std::shared_ptr<SomeWidelyUsedClass>template class std::shared_ptr<SomeWidelyUsedClass>shared_ptr<SomeWidelyUsedClass>shared_ptr<memory>extern templateshared_ptr

澄清:

// stdafx.h; included in every cpp in the project
#include <memory>
#include "SomeWidelyUsedClass.h" // no shared_ptr in here

// I expect this to prevent instantiation of std::shared_ptr<SomeWidelyUsedClass>
// in all compilation units that include this, except the one below.
extern template class std::shared_ptr<SomeWidelyUsedClass>;
Run Code Online (Sandbox Code Playgroud)

然后:

// ExplicitTemplateInstantiations.cpp
#include "stdafx.h"

// I expect this to cause std::shared_ptr<SomeWidelyUsedClass>
// to be instantiated in …
Run Code Online (Sandbox Code Playgroud)

c++ templates visual-c++ explicit-specialization visual-studio-2012

5
推荐指数
1
解决办法
552
查看次数

在同一对象上创建多个shared_ptr"系列"时,shared_from_this的意外行为

这是一些示例代码(在线):

#include <memory>

struct Foo : public std::enable_shared_from_this<Foo> {};

void example()
{
    auto sharedFoo = std::make_shared<Foo>();
    std::shared_ptr<Foo> nonDeletingSharedFoo(sharedFoo.get(), [](void*){});
    nonDeletingSharedFoo.reset();
    sharedFoo->shared_from_this(); // throws std::bad_weak_ptr
}
Run Code Online (Sandbox Code Playgroud)

我所看到的(在多个编译器下)是当nonDeletingSharedFoo重置时,weak_ptr内部使用的是enable_shared_from_thisexpires,因此后续调用shared_from_this失败.

我希望nonDeletingSharedFoo有一个完全独立的引用计数,sharedFoo因为它是从原始指针构造的,但显然它仍然影响Foo对象内部的弱计数weak_ptr.我假设这是因为shared_ptr构造函数和/或析构函数在指向类型实现时执行了一些特殊操作enable_shared_from_this.

那么这段代码违反了标准吗?有没有解决方案,或者只是不可能在shared_ptr实现的对象上有多个"系列" enable_shared_from_this

c++ shared-ptr weak-ptr

5
推荐指数
1
解决办法
426
查看次数

为什么*没有*ReSharper告诉我"隐式捕获关闭"?

这个问题及其答案很好地解释了隐式捕获闭包的概念.但是,我偶尔会看到代码似乎应该生成有问题的警告,实际上并没有.例如:

public static void F()
{
  var rnd1 = new Random();
  var rnd2 = new Random();
  Action a1 = () => G(rnd1);
  Action a2 = () => G(rnd2);
}

private static void G(Random r)
{
}
Run Code Online (Sandbox Code Playgroud)

我的期望是,我会被警告,a1隐含地捕获rnd2,并a2隐含地捕获rnd1.但是,我根本没有得到任何警告(链接问题中的代码确实为我生成了它).这是ReSharper的一个错误(v9.2),还是由于某种原因不会在这里发生隐式捕获?

c# resharper lambda closures resharper-9.2

5
推荐指数
1
解决办法
643
查看次数

在ControlTemplate的DataTrigger中使用时,TemplatedParent为null

考虑这个(编辑降)Style,设计用于ButtonContentString:

<Style x:Key="Test" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
               <StackPanel>
                   <TextBlock x:Name="text" Text="{TemplateBinding Content}" />
                   <TextBlock x:Name="demo" Text="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}">
                        <DataTrigger.Value>
                            <system:String>Test</system:String>
                        </DataTrigger.Value>
                        <Setter TargetName="test" Property="Foreground" Value="Red" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

此示例中的意图是,如果按钮文本等于单词"Test" 1,则将其变为红色.但是,这是行不通的,因为触发的TemplatedParent绑定解析为空,而不是到ButtonStyle被应用到.但是,TextBlock命名的"demo"将按Text预期设置为"System.Windows.Controls.Button:[ButtonText]",这意味着TemplatedParent在该级别上正常工作.为什么它不在里面工作DataTrigger


1我知道还有其他方法可以实现这一点,但我试图理解为什么绑定不像我期望的那样工作.

c# wpf xaml controltemplate wpf-style

5
推荐指数
1
解决办法
746
查看次数

remove_pointer类似物,适用于支持运算符*的所有内容

我想要的语义与std::remove_pointer真指针相似但可用于指针类的东西。我自然可以列举出已知的可能性:

// Important: remove_pointer_ex<T>::type should evaluate to T
// if T::operator* is not defined (like std::remove_pointer)
template<typename T> struct remove_pointer_ex { typedef T type; };

template<typename T> struct remove_pointer_ex<T*> { typedef T type; };
template<typename T> struct remove_pointer_ex<std::shared_ptr<T>> { typedef T type; };
template<typename T> struct remove_pointer_ex<std::unique_ptr<T>> { typedef T type; };
// and then again for all the different cv combinations
Run Code Online (Sandbox Code Playgroud)

但我真的很想一种可以与任何支持类一起使用的方法operator*

看起来这应该可以使用SFINAE和/或类型特征来实现。这个问题的答案描述了如何测试特定类型是否具有特定成员,我想我可以结合使用这些建议之一enable_if,但是坦率地说,如果没有不那么丑陋的方法来解决问题,我宁愿只是尝试一种完全不同的方法。

c++ templates sfinae type-traits c++11

3
推荐指数
1
解决办法
345
查看次数

Reactive的"缓冲直到安静"行为?

我的问题有点像Nagle算法创建的问题,但不完全正确.我想要的是将OnNext通知缓冲IObservable<T>到一系列IObservable<IList<T>>s中,如下所示:

  1. 第一个T通知到达时,将其添加到缓冲区并开始倒计时
  2. 如果T在倒计时到期之前收到另一个通知,请将其添加到缓冲区并重新开始倒计时
  3. 一旦倒计时结束(即生产者已经沉默了一段时间),将所有缓冲的T通知转发为单个聚合IList<T>通知.
  4. 如果缓冲区大小在倒计时到期之前超过某个最大值,则无论如何都要发送它.

IObservable<IList<T>> Buffer(this IObservable<T>, Timespan, int, IScheduler) 看起来很有希望,但它似乎定期发送聚合通知,而不是"在第一个通知到达时启动计时器,当其他通知到达时重启它"我想要的行为,并且它还发送一个空列表在如果没有从下面生成通知,则每个时间窗口的结束.

希望放弃任何的T通知; 只是缓冲它们.

有这样的事情存在,还是我需要自己编写?

c# reactive-programming system.reactive

3
推荐指数
2
解决办法
784
查看次数

构造函数定义可以使用"class"关键字作为前缀吗?

请告诉我为什么我的程序被编译和执行时,我将构造函数视为没有私有和公共部分的类,只需class在其前面写下单词,如下所示:

class sample
{
private:
    int a,b;

public:
    class sample(int a1){a = a1;}
};
Run Code Online (Sandbox Code Playgroud)

c++ compiler-bug visual-studio-2012 visual-studio-2013

2
推荐指数
1
解决办法
101
查看次数

Copy SVG image stored as XML string to Windows clipboard as image/svg+xml MIME type

I have an SVG image stored as a .net XML string. If I write that string to a file, I can load it into SVG editors without any trouble, so I know its contents are good. But what I want to do is place it in the Windows clipboard as the image/svg+xml MIME type. I've tried the following:

string svg = GetSvg();
byte[] bytes = Encoding.UTF8.GetBytes(svg);

Clipboard.SetData("image/svg+xml", svg); // idea 1
Clipboard.SetData("image/svg+xml", bytes); // idea 2
Run Code Online (Sandbox Code Playgroud)

Based on my …

c# windows clipboard svg

2
推荐指数
1
解决办法
1239
查看次数

为什么即使我没有在任何地方使用MediaPlayer,偶尔也会在logcat中看到"MediaPlayer未经发布而最终确定"?

我,但是,使用RingtoneManager得到Ringtone秒.


此信息已包含在此问题和答案中,但Google员工不太可能找到它,除非他们已经知道要搜索的内容(至少我不能).

android ringtone android-logcat android-mediaplayer

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

lambda中的虚拟参数将匹配任何类型?

假设我有这样的事情:

struct Foo {};
struct Bar {};
struct Baz {};

// ...

void RegisterListener1(std::function<void(Foo)>);
void RegisterListener2(std::function<void(Bar)>);
void RegisterListener3(std::function<void(Baz)>);
Run Code Online (Sandbox Code Playgroud)

哪里有没有关系Foo,BarBaz.

现在,假设我想将这些寄存器函数中的每一个传递给一个忽略其参数的相同lambda .有什么我可以放在lambda的参数列表中,这意味着"在这里匹配任何东西;我只是想把这个东西扔掉"?

auto listener = []( /* what? */ ) { throw UnsupportedOperationException(); };
RegisterListener1( listener );
RegisterListener2( listener );
RegisterListener3( listener );
Run Code Online (Sandbox Code Playgroud)

我可以使用函数模板而不是lambda,并执行以下操作:

template<typename T>
void listener(T)
{
   throw UnsupportedOperationException();
}

// ...

RegisterListener1( listener<Foo> );
RegisterListener2( listener<Bar> );
RegisterListener3( listener<Baz> );
Run Code Online (Sandbox Code Playgroud)

但这很乏味,特别是如果三个Register函数的仿函数参数都是模板化的,那么就没有简单的方法可以写出"内部"参数类型.这是我在打字过程中遇到的另一个想法:

struct Anything
{
    template<typename T> Anything(const T&) {}
}; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda

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