我需要一些来自c ++代码的外部程序调用的帮助.
我必须javap.exe多次(可能超过100)从我的程序中调用(来自JDK包),但调用system("javap.exe some_parameters")非常慢.对于一组参数,它的工作非常好,但重复的调用是system()不可接受的.我认为这只是因为访问硬盘和应用程序运行的成本(但我不确定).
我能做些什么才能获得更好的表现?我可以"保存javap.exe在RAM中"并"直接"调用它.或者可能有人知道我怎么能获得java级的描述和方法签名javap.exe?
我正在用C ++为新容器编写迭代器。当我调用begin()和end()函数(都很短)时,我希望编译器将其内联。但事实并非如此。我认为可能是因为迭代器作为临时对象返回,并且需要将其复制。我想也许我可以在c ++ 11中使用RValue引用,但是我对此并不熟悉。那么有没有办法使其内联?
代码是这样的(不完全相同,但是我认为如果此版本有效,我的代码也将有效)?
我正在使用VC ++ 2013 CTP,并且更改了编译选项,但是它也不起作用。
class Pointer
{
public:
Pointer(int* p) : p(p)
{
(*p)++;
}
Pointer(const Pointer& pointer) : p(pointer.p)
{
(*p)++;
}
Pointer& operator =(const Pointer& pointer)
{
(*p)--;
p = pointer.p;
(*p)++;
}
int* p;
~Pointer()
{
(*p)--;
}
static Pointer create(int& p)
{
return Pointer(&p);
}
static Pointer create2(int& p)
{
return create(p);
}
};
int main()
{
int p = 0;
Pointer pointer = Pointer::create2(p);
}
Run Code Online (Sandbox Code Playgroud)
您可以看到,这里的函数create和create2不是内联的,即使您确实很简单。
我知道这可能不会影响程序速度,但我只是想使其更好。
在www.CPlusPlus.com上,它说明了unordered_multimap的以下内容,
具有等效键的元素在同一个桶中组合在一起,并且迭代器(请参阅equal_range)可以遍历所有这些元素.
我知道我们无法从该语句中推断出这一点,但我想知道给定的存储桶是否只包含具有等效键的元素?
我可以在里面做到这一点decltype():
auto g() -> decltype(1, "", true, new int);
Run Code Online (Sandbox Code Playgroud)
但不是这个:
template <class... Args>
auto g(Args&&... args) -> decltype(args...);
Run Code Online (Sandbox Code Playgroud)
它失败了,因为包内扩展出现在里面,decltype()但我认为包扩展会导致以逗号分隔的参数列表.因此返回类型g(a, b, c)将是decltype(c)因为逗号运算符的工作原理(它返回最后一个元素).当你在函数参数列表,模板参数列表,初始化列表等内部扩展时,它可以工作.但为什么这不是这种情况?
我开始阅读模板,我发现智能指针使用双模板,如下所示:
template <class T>
class myclass
{
public:
template <class U>
myclass(U* q) { /* ... */ }
};
Run Code Online (Sandbox Code Playgroud)
那是什么意思?我知道模板功能会推断U,如
myclass(new whatever(3));
Run Code Online (Sandbox Code Playgroud)
U会在哪里whatever*.那是什么T?U和之间有什么关系T?
我糊涂了...
假设我有一个基类,以后可以通过派生它来"扩展",让我们调用这个类Base和扩展Derived.类的模板签名是固定的,不能更改(即我们不能更改类的模板参数).Derived该类的作者一无所知Base,只是它的构造函数可能需要一些参数.
但是,最终派生类的调用者知道应该传递多少个参数.我怎么写这个Derived扩展名?这是我有的:
struct Base
{
Base(int baseArg) {}
};
struct Derived : public Base
{
template <typename... Args>
Derived(Args&&... args, int derivedArg)
:
Base(std::forward<Args>(args)...)
{
}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,Derived d(1, 1);我得到以下erorr消息:
prog.cpp: In function 'int main()':
prog.cpp:19:16: error: no matching function for call to 'Derived::Derived(int, int)'
Derived d(1, 1);
^
prog.cpp:19:16: note: candidates are:
prog.cpp:11:2: note: template<class ... Args> Derived::Derived(Args&& ..., int)
Derived(Args&&... …Run Code Online (Sandbox Code Playgroud) 我有一个关于模板函数与函数自动类型推导的一般性问题.
多年来,我们已经能够编写模板功能:
template <class T> T add(T a,T b){
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
有一个TS用于使用auto来进行函数的参数推导
auto add(auto a,auto b){
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
我虽然使用auto,但是没有办法获得实际类型,例如使用静态成员,但这样做很好:
#include <iostream>
struct foo
{
static void bar(){std::cout<<"bar"<<std::endl;}
static int i ;
};
int foo::i{0};
void t(auto f){
decltype(f)::bar();
std::cout<< decltype(f)::i<<std::endl;
}
int main(int argc, char *argv[])
{
t(foo());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么有没有理由选择一个而不是另一个?
我想用可变参数模板编写线性叠加的抽象.为此,我想定义一个表示某种形式的operator()的基类型
template <typename Result, typename... Parameters>
class Superposable {
public:
typedef Result result_t;
void operator()(Result& result, const Parameters&...) const = 0;
};
Run Code Online (Sandbox Code Playgroud)
然后从它继承当前的问题,例如像这样
class MyField : public Superposable<double, double, double> {
public:
void operator()(double& result, const double& p1, const double& p2) const {
result = p1 + p2;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我想编写一个抽象基类,它可以形成线性叠加,并将Superposable派生类作为模板参数来确定operator()的调用签名.我想有类似的东西
template<typename S> // where S must be inherited from Superposable
class Superposition {
private:
std::vector< std::shared_ptr<S> > elements;
public:
// This is the problem. How do I do this? …Run Code Online (Sandbox Code Playgroud) 我的Boost标头的本地版本(1.56.0)定义了以下函数boost/any.hpp,逐字复制:
// Note: The "unsafe" versions of any_cast are not part of the
// public interface and may be removed at any time. They are
// required where we know what type is stored in the any and can't
// use typeid() comparison, e.g., when our types may travel across
// different shared libraries.
template<typename ValueType>
inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
{
return &static_cast<any::holder<ValueType> *>(operand->content)->held;
}
template<typename ValueType>
inline const ValueType * unsafe_any_cast(const any * operand) …Run Code Online (Sandbox Code Playgroud) 我试图std::bind去一个lambda时遇到了错误.以下代码示例编译正常:
#include <functional>
#include <iostream>
int main()
{
const auto lambda1 = [](int a, int b){ return a + b; };
std::cout << (std::bind( lambda1, std::placeholders::_1, 2))(2)
<< std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但以下不是:
#include <functional>
#include <iostream>
int main()
{
const auto lambda2 { [](int a, int b){ return a + b; } }; // Note the "{ }-initialization"
std::cout << (std::bind( lambda2, std::placeholders::_1, 2))(2)
<< std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我使用Visual Studio 2013得到的错误输出,更新4:
1>------ Build started: Project: Project1, …Run Code Online (Sandbox Code Playgroud)