考虑一下我正在写一个静态库.让它有一个班级Foo
// mylib.h
#include <dependency_header_from_other_static_library.h>
class Foo {
// ...
private:
type_from_dependent_library x;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这个库(让它调用它mylib)取决于另一个库.它汇编得很好.但是当用户编译它的代码(使用Foo和包含mylib.h)并与我的lib链接时,编译失败,因为用户也需要有dependency_header_from_other_static_library.h头文件来编译代码.
我想隐藏用户的这种依赖.怎么做到这一点?想到的一件事是PIMPL成语.喜欢:
// mylib.h
#include <dependency_header_from_other_static_library.h>
class Foo {
// ...
private:
class FooImpl;
boost::shared_ptr<FooImpl> impl_;
}
// mylib_priv.h
class FooImpl {
// ...
private:
type_from_dependent_library x;
}
Run Code Online (Sandbox Code Playgroud)
但是,它需要我来复制类的接口Foo在FooImpl.而且,PIMPL在我的案例中使用是否过度杀伤?
谢谢.
考虑以下课程
class Foo
{
typedef bool (*filter_function)(Tree* node, std::list<std::string>& arg);
void filter(int filter, std::list<std::string>& args)
{
...
if (filter & FILTER_BY_EVENTS) {
do_filter(events_filter, args, false, filter & FILTER_NEGATION);
}
...
}
void do_filter(filter_function ff, std::list<std::string>& arg,
bool mark = false, bool negation = false, Tree* root = NULL)
{
...
}
bool events_filter(Tree* node, std::list<std::string>& arg)
{
...
}
};
Run Code Online (Sandbox Code Playgroud)
我可以events_filter作为参数传递给do_filter唯一events_filter的static成员.但我不想成功static.有没有办法可以将指向成员函数的指针传递给另一个函数?可能正在使用boost库(如函数)左右.
谢谢.
我正在尝试采用使用的旧代码(现已弃用)WebSocketServlet.旧代码如下所示:
@Singleton
ExampleServlet extends WebSocketServlet {
@Override
protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
// Do something
// ...
return // StreamInbound impl;
}
}
Run Code Online (Sandbox Code Playgroud)
正如我在较新版本的tomcat 7中所说,从tomcat 8(WebSocket 1.0,Tyrus)向后移植了WebSocket实现,并且WebSocketServlet不推荐使用类.
我应该用什么代替用更新的API部署我的servlet?
有办法:
1)删除索引处的项目:
// Removes item at index N, e.g. Remove<2, a, b, c, d> results in <a, b, d>
template<std::size_t N, typename ...Args>
struct Remove {
// ???
};
Run Code Online (Sandbox Code Playgroud)
2)替换索引处的项目:
// Replaces item at index N with T, e.g. Replace<2, x, a, b, c, d> results in <a, b, x, d>
template<std::size_t N, typename T, typename ...Args>
struct Replace {
// ???
};
Run Code Online (Sandbox Code Playgroud)
3)更换范围内的物品
// Replaces items in range [N1, N2] with T, e.g. ReplaceRange<2, 3, x, a, b, c, …Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming variadic-templates c++11
我的问题与此类似,但我有两个字符串(as char *),任务是strnicmp用类似的东西替换函数(仅适用于MS VC)boost::iequals.
注意strnicmp不是stricmp- 它只比较前n个字符.
有没有比这更简单的解决方案:
void foo(const char *s1, const char *s2)
{
...
std::string str1 = s1;
std::string str2 = s2;
int n = 7;
if (boost::iequals(str1.substr(0, n), str2)) {
...
}
}
Run Code Online (Sandbox Code Playgroud) 考虑我有以下课程:
/// File classes.d
class C {
string type() { return "C"; };
}
class C1 : C {
override string type() { return "C1"; };
}
class C2 : C {
override string type() { return "C1"; };
}
Run Code Online (Sandbox Code Playgroud)
现在我想在其他地方实施工厂,例如:
/// File factory.d
module factory;
import std.functional;
import std.stdio;
void main() {
mixin(import("classes.d"));
auto c = cast(typeof(mixin("C1"))) Object.factory("C1");
writeln(c.type());
}
Run Code Online (Sandbox Code Playgroud)
编译器(dmd 2.058)说:
factory.d(7): Error argument C1 to typeof is not an expression
Run Code Online (Sandbox Code Playgroud)
我知道以下行汇编得很好:
auto c = cast(C1) Object.factory("classes.C1");
Run Code Online (Sandbox Code Playgroud)
但这需要我在编译时知道类型(C1).我想在运行时获取类型(如字符串).
我用来java.util.Scanner从控制台读取命令.
try
{
ICommand cmd = cmdReader.ReadCommand();
cmd.Execute(conn);
}
catch(MyException ex)
{
// print a message about unknown command
continue;
}
Run Code Online (Sandbox Code Playgroud)
在哪里ReadCommand实施如下:
try (Scanner scanIn = new Scanner(System.in))
{
if (!scanIn.hasNextLine()) return null;
line_ = scanIn.nextLine();
ICommand command = parser_.ParseCommand(line_);
return command;
}
Run Code Online (Sandbox Code Playgroud)
在第一次迭代中代码工作正常,我写了一些无效的东西(不是命令),代码打印警告并继续.但即使我在控制台中写东西,其他迭代也会返回null此处if (!scanIn.hasNextLine()) return null;.看起来java.util.Scanner没有看到输入.难道我做错了什么?然后我怎么可以等待用户输入(不想使用循环sleep)?
我正在尝试adjtime调整时钟(例如,未来 15 分钟),为adjtime功能提供正确的偏移。但据我了解,adjtime速度非常慢。所以当我实际调用该函数然后检查时间时它保持不变。
如何做一些小的调整(例如1秒到1小时)以便效果立即显现?
谢谢。
c++ ×4
java ×2
adjustment ×1
boost ×1
c ×1
c++11 ×1
d ×1
dependencies ×1
factory ×1
mixins ×1
pimpl-idiom ×1
servlet-3.0 ×1
stl ×1
string ×1
templates ×1
time ×1
tomcat7 ×1
websocket ×1