C++的强制转换static_cast,const_cast,reinterpret_cast有一个模板的语法,如
long foo = 3;
int bar = static_cast<int>(foo);
Run Code Online (Sandbox Code Playgroud)
我看过标准版,它说演员表是表达式,而不是我想的模板函数.
这让我想知道:在幕后,这些演员阵容只是具有特权状态的模板,还是他们碰巧借用模板语法的关键字?
我正在尝试使用继承和模板参数推导std::shared_ptr.正如您在下面的示例代码中所看到的,我正在传递shared_ptr<Derived>一个模板化的非成员函数,该函数应该进行模板参数推导.如果我手动命名类型一切正常,如果我让它做模板参数推导它没有.它会看起来好像编译器无法弄清楚的类型,但该错误消息表明,它没有.我不确定这里发生了什么,我将不胜感激.(Visual Studio 2010)
#include <memory>
template <typename T>
class Base {};
class Derived : public Base<int> {};
template <typename T>
void func(std::shared_ptr<Base<T> > ptr) {};
int main(int argc, char* argv[])
{
std::shared_ptr<Base<int>> myfoo(std::shared_ptr<Derived>(new Derived)); // Compiles
func(myfoo); // Compiles
func<int>(std::shared_ptr<Derived>(new Derived)); // Compiles
func(std::shared_ptr<Derived>(new Derived)); // Doesn't compile. The error message suggests it did deduce the template argument.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息:
5> error C2664: 'func' : cannot convert parameter 1 from 'std::tr1::shared_ptr<_Ty>' to …Run Code Online (Sandbox Code Playgroud) 在使用来执行大量文件I / O的某些代码中std::ofstream,我在缓存流以提高效率。但是,有时我需要更改文件的打开模式(例如,追加与截断)。这是一些类似的模拟代码:
class Logger {
public:
void write(const std::string& str, std::ios_base::openmode mode) {
if (!myStream.is_open) myStream.open(path.c_str(), mode);
/* Want: if (myStream.mode != mode) {
myStream.close();
myStream.open(path.c_str(), mode);
}
*/
myStream << str;
}
private:
std::ofstream myStream;
std::string path = "/foo/bar/baz";
}
Run Code Online (Sandbox Code Playgroud)
有谁知道:
ofstream?的打开模式。openmodean 的电流,ofstream以便仅在必要时关闭并重新打开它?我有一个基于JSON数据的工作JSTree,复选框插件显示每个项目旁边的框 - 到目前为止一切都很好.现在我想知道用户检查了哪些节点,所以我可以对数据做些什么.
不幸的是,我还没有找到通过文档或网络搜索来实现这一目标的方法.关于SO的一些答案说使用get_checked方法,但要么我真的缺少某些东西,要么我的JSTree版本没有那个(即该字符串不会出现在项目文件中的任何地方).我的版本是3.0.0,这是最新版本.
有人可以指点我a)如何获取检查项目,或b)如何访问树的一些可迭代表示,以便我自己抓住它们?
(我应该提到我对Javascript很新.)
以下是我如何设置树,它基于网站上的文档:
var treeInit = { core: { data : [
/* all my data */
]
}};
treeInit.plugins = ["checkbox"];
$('tree_div').jstree(treeInit);
Run Code Online (Sandbox Code Playgroud)