我有这个示例程序:
#include <iostream>
template<typename Message, typename Decoration, typename PrintImpl>
void print_surrounded(Message&& msg, const Decoration& decoration, const PrintImpl& print_impl)
{
print_impl(decoration); // should forward be used?
print_impl(" ");
print_impl(std::forward<Message>(msg));
print_impl(" ");
print_impl(decoration);
}
template<typename Message, typename PrintImpl>
void pretty_print(Message&& msg, const PrintImpl& print_impl)
{
print_surrounded(std::forward<Message>(msg), "***", print_impl);
}
int main()
{
pretty_print("So pretty!", [](const char* msg) {
std::cout << msg;
});
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用不同的方式传递参数:
&&吗?如果是,我还应该使用std::forward吗?)我做出了正确的选择吗?
我正在学习monads并且有一些问题.
这就是我现在所处的位置.请纠正我错在哪里.
该>>=标志为中缀运算符.中缀运算符是带有两个参数(左侧和右侧)并返回值的函数.
该>>=符号称为绑定运算符并具有签名Monad m => m t -> (t -> m u) -> m u.但是,这些类型似乎没有排在这里.我们得到一个类型的值,m t第二个参数是一个带有a的函数t.(我不知道如何连接点.)
这必定意味着绑定功能,在某种程度上能够去除m从m t为了得到t并把它传递给函数.
这是我的问题:
是否能够m从m t仅在此类绑定运算符内部执行的操作中删除.这个绑定运算符是否有一些特殊的特权?
它与状态变化有什么关系?我理解(我认为)monad的目标是"包装"副作用,以便它们与程序的其余部分隔离开来.但是绑定运算符在这方面的作用是什么?
经过几个周末探索Clojure后,我想出了这个程序.它允许您在窗口中移动一个小矩形.这是代码:
(import java.awt.Color)
(import java.awt.Dimension)
(import java.awt.event.KeyListener)
(import javax.swing.JFrame)
(import javax.swing.JPanel)
(def x (ref 0))
(def y (ref 0))
(def panel
(proxy [JPanel KeyListener] []
(getPreferredSize [] (Dimension. 100 100))
(keyPressed [e]
(let [keyCode (.getKeyCode e)]
(if (== 37 keyCode) (dosync (alter x dec))
(if (== 38 keyCode) (dosync (alter y dec))
(if (== 39 keyCode) (dosync (alter x inc))
(if (== 40 keyCode) (dosync (alter y inc))
(println keyCode)))))))
(keyReleased [e])
(keyTyped [e])))
(doto panel
(.setFocusable true)
(.addKeyListener panel)) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个报告本地计算机上网络设备属性的应用程序.我需要mac地址,mtu,链接速度和其他一些.我正在使用udev.我已经弄清楚如何获取mac地址和mtu,但不知道如何获得链接速度.我可以从终端使用ethtool获取它,但我需要一种方法来以编程方式获取它.
有谁知道如何使用udev或其他库获取链接速度属性?
我喜欢创建具有给定大小和值的向量,例如:
std::vector<std::string> names(10);
Run Code Online (Sandbox Code Playgroud)
然而,这几次导致了意想不到的结果.例如,在以下代码中,每个代码都UniqueNumber具有相同的值:
#include <iostream>
#include <string>
#include <vector>
struct UniqueNumber
{
UniqueNumber() : mValue(sInstanceCount++)
{
}
inline unsigned int value() const
{
return mValue;
}
private:
static unsigned int sInstanceCount;
unsigned int mValue;
};
int UniqueNumber::sInstanceCount(0);
int main()
{
std::vector<UniqueNumber> numbers(10);
for (size_t i = 0; i < numbers.size(); ++i)
{
std::cout << numbers[i].value() << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
0 0 0 0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
在查看std :: vector的构造函数时它确实有意义:
explicit vector(size_type __n, …Run Code Online (Sandbox Code Playgroud) 我正在阅读有关SFINAE的维基百科文章,并遇到以下代码示例:
struct Test
{
typedef int Type;
};
template < typename T >
void f( typename T::Type ) {} // definition #1
template < typename T >
void f( T ) {} // definition #2
void foo()
{
f< Test > ( 10 ); //call #1
f< int > ( 10 ); //call #2 without error thanks to SFINAE
}
Run Code Online (Sandbox Code Playgroud)
现在我实际上已经编写了这样的代码,并且在某种程度上直觉上我知道我需要输入"typename T"而不是"T".但是,了解它背后的实际逻辑会很高兴.有人在乎解释吗?
我想使用Qt创建一个可以播放本地视频文件的简单GUI应用程序.我可以使用Phonon完成幕后的所有工作,但我需要更多的控制.我已经使用decodebin和autovideosink元素成功实现了GStreamer管道.现在我想使用Qt小部件将输出引导到.
有没有人成功过这样做?(我想是因为有基于Qt的视频播放器建立在GStreamer之上.)有人能指出我正确的方向如何做到这一点吗?
注意:此问题类似于我之前发布的有关如何将Qt与传入RTP流连接的问题.这似乎非常具有挑战性.我想这个问题会更容易回答.
Patrice建议使用libVLC非常有用.这是VLC网站上的代码的一个更简洁的版本: Qt + libVLC的示例.但是,我原来的问题仍然存在:如何将GStreamer连接到Qt小部件?
经过一些实验,我最终得到了这个工作样本.它取决于我自己的小GstSupport库中的GstWidget.h和GstWidget.cpp.但请注意,目前仅在Mac版的Qt上进行了测试.
有一段时间,一位同事告诉我,他花了很多时间来调试竞争条件.罪魁祸首竟然是这样的:
void foo()
{
ScopedLock(this->mutex); // Oops, should have been a named object.
// Edit: added the "this->" to fix compilation issue.
// ....
}
Run Code Online (Sandbox Code Playgroud)
为了防止情况再次发生,他在定义ScopedLock类之后创建了以下宏:
#define ScopedLock(...) Error_You_should_create_a_named_object;
Run Code Online (Sandbox Code Playgroud)
这个补丁工作正常.
有没有人知道任何其他有趣的技术来防止这个问题?
这是我发布此程序的这个问题的后续行动:
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
#include <chrono>
class Stopwatch
{
public:
typedef std::chrono::high_resolution_clock Clock;
//! Constructor starts the stopwatch
Stopwatch() : mStart(Clock::now())
{
}
//! Returns elapsed number of seconds in decimal form.
double elapsed()
{
return 1.0 * (Clock::now() - mStart).count() / Clock::period::den;
}
Clock::time_point mStart;
};
struct test_cast
{
int operator()(const char * data) const
{
return *((int*)data);
}
};
struct test_memcpy
{
int …Run Code Online (Sandbox Code Playgroud) 在下面的示例代码中,我想Item从以下内容创建一个对象Component:
struct Component { };
struct Item {
explicit Item(Component component) : comp(component) {}
Component comp;
};
struct Factory {
static std::future<Item> get_item() {
std::future<Component> component = get_component();
// how to get a std::future<Item> ?
}
std::future<Component> get_component();
};
Run Code Online (Sandbox Code Playgroud)
如何从去std::future<Component>到std::future<Item>?
更新:从问题中删除了我的第一个想法(基于线程)并发布了答案.