英文:
What is mutex_wrapped?
问题
In this tutorial, they discuss the usage of generic lambdas in C++. The first code snippet demonstrates visiting syntax:
boost::variant<int, double> value;
apply_visitor(value, [&](auto&& e){
std::cout << e;
});
The second code snippet involves a mutex_wrapped
class, and you're correct; it appears to be an imaginary class with a write
method that takes a lambda and executes it using the wrapped stream as a parameter. This part of the example may not be representative of standard C++ usage.
For your first observation, it seems like there might have been changes to the Boost library that required swapping the operands in the boost::apply_visitor
call:
boost::variant<int, double> value;
boost::apply_visitor(
[&](auto&& e){
std::cout << e;
},
value
);
However, as you mentioned, it's possible that the tutorial's content has become outdated since it was written.
英文:
In this tutorial they state:
> A strong reason to use generic lambdas is for visiting syntax.
>
> boost::variant<int, double> value;
> apply_visitor(value, [&](auto&& e){
> std::cout << e;
> });
>
> Here we are visiting in a polymorphic manner; but in other contexts,
> the names of the type we are passing isn't interesting:
>
> mutex_wrapped<std::ostream&> os = std::cout;
> os.write([&](auto&& os){
> os << "hello world\n";
> });
>
> Repeating the type of std::ostream& is noise here; it would be like
> having to mention the type of a variable every time you use it. Here
> we are creating a visitor, but no a polymorphic one; auto is used for
> the same reason you might use auto in a for(:) loop.
First of all I could not get the above example to work with out swapping the operands around:
boost::variant<int, double> value;
boost::apply_visitor(
[&](auto&& e){
std::cout << e;
},
value
);
I suppose this could be due to changes to boost since the tutorial was written.
But for the second example I cannot yet find from where mutex_wrapped
comes, and I do not understand what write(lambda)
will do, it seems to me that write()
requires const char* s, streamsize n
.
Is this second example purely imaginary as in mutex_wrapped is an imaginary class that has a write method that takes a lambda and executes the lambda using the wrapped stream as the parameter?
答案1
得分: 0
免责声明该网站:
本教程是从StackOverflow文档编译而来,内容由Stack Overflow的杰出人士撰写。
通过在StackOverflow上搜索,我找到了这个发布的答案:https://stackoverflow.com/a/59601498/6752050。它定义了shared_mutex_wrapped
,但示例中将其用作mutex_wrapped
,这可能是一个拼写错误。让我们将其命名为mutex_wrapped
:
template<class T>
struct mutex_wrapped {
mutex_wrapped(T t) : t(t) {} // 在原始帖子中缺少此部分
template<class F>
auto read( F&& f ) const {
auto l = lock();
return f(t);
}
template<class F>
auto write( F&& f ) {
auto l = lock();
return f(t);
}
private:
mutable std::shared_mutex m;
T t = {};
auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};
英文:
Declaimer of that site:
> This Tutorial is compiled from StackOverflow Documentation, the content is written by the brilliant people at Stack Overflow.
Googling trough StackOverflow gave me that posted answer: https://stackoverflow.com/a/59601498/6752050. It defined shared_mutex_wrapped
but the example used it as mutex_wrapped
, it might be a typo. Let's name it mutex_wrapped
:
template<class T>
struct mutex_wrapped {
mutex_wrapped(T t) : t(t) {} // Was missing in the original post
template<class F>
auto read( F&& f ) const {
auto l = lock();
return f(t);
}
template<class F>
auto write( F&& f ) {
auto l = lock();
return f(t);
}
private:
mutable std::shared_mutex m;
T t = {};
auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};
答案2
得分: 0
The Wrapper needs a generic constructor to assign its internal type T
, t
variable. Here's a complete corrected example:
#include <iostream>
#include <mutex>
#include <shared_mutex>
template<class T>
struct mutex_wrapped {
explicit mutex_wrapped(T t) : t(t) {}
template<class F>
auto read( F&& f ) const {
auto l = lock();
return f(t);
}
template<class F>
auto write( F&& f ) {
auto l = lock();
return f(t);
}
private:
mutable std::shared_mutex m;
T t = {};
auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};
int main() {
mutex_wrapped<std::ostream&&> os(std::cout);
os.write([&](auto&& os){
os << "hello world\n";
});
return 0;
}
英文:
The Wrapper needs a generic constructor to assign it's internal type T
, t
variable. Here's a complete corrected example:
#include <iostream>
#include <mutex>
#include <shared_mutex>
template<class T>
struct mutex_wrapped {
explicit mutex_wrapped(T t) : t(t) {}
template<class F>
auto read( F&& f ) const {
auto l = lock();
return f(t);
}
template<class F>
auto write( F&& f ) {
auto l = lock();
return f(t);
}
private:
mutable std::shared_mutex m;
T t = {};
auto lock() const { return std::shared_lock< std::shared_mutex >( m ); }
auto lock() { return std::unique_lock< std::shared_mutex >( m ); }
};
int main() {
mutex_wrapped<std::ostream&> os(std::cout);
os.write([&](auto&& os){
os << "hello world\n";
});
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论