给定一个典型的函数,它将C-Functionpointer作为C-Stdlib之类的回调qsort(),任何编译器都可以使用内联来优化代码吗?我认为不可以,这是正确的吗?
int cmp(void* pa, void* pb) { /*...*/ }
int func() {
int vec[1000];
qsort(vec, 1000, sizeof(int), &cmp);
}
Run Code Online (Sandbox Code Playgroud)
好的,qsort()是来自外部库的功能,但我不认为即使LTO在这里会有所帮助,对吧?
但是,如果我my_qsort()在同一个编译单元中定义,那么编译器可以内联吗?
int cmp(void* pa, void* pb) { /*...*/ }
void my_qsort(int* vec, int n, int sz, (void*)(void*,void*)) { /* ... */ }
int func() {
int vec[1000];
my_qsort(vec, 1000, sizeof(int), &cmp);
}
Run Code Online (Sandbox Code Playgroud)
这有什么不同吗?我认为使用C函数指针作为回调是阻止编译器内联的因素.正确?
(我只想确保理解为什么我应该在C++中使用Functors)
我想知道我emplace_back是否理解正确
#include <vector>
using namespace std;
struct Hero {
Hero(const string&) {}
Hero(const char*) {}
Hero(int) {}
// forbid a clone:
Hero(const Hero&) = delete;
Hero& operator=(const Hero&) = delete;
};
int main() {
vector<Hero> heros1 = { "Bond", "Hulk", "Tarzan" }; // ERR: copies?
vector<Hero> heros;
heros.emplace_back( 5 ); // ERR: copies
heros.emplace_back( string("Bond") ); // ERR: copies
heros.emplace_back( "Hulk" ); // ERR: copies
}
Run Code Online (Sandbox Code Playgroud)
因此,我真的很想知道如果我理解emplace_back不正确:我虽然会阻止复制Hero,因为它会就地创建项目.
或者它是我的g ++ - …
我有一个文件 module.hpp
struct ModuleBase {
virtual void run() = 0;
};
Run Code Online (Sandbox Code Playgroud)
和一个main.cpp程序
int main() {
cout << ...?...; // here should go the contents of module.hpp
}
Run Code Online (Sandbox Code Playgroud)
我可以用什么来...?...打印头文件的内容?
一个基本的想法是
int main() {
static const string content = R"(
#include <module.hpp>
)";
cout << content;
}
Run Code Online (Sandbox Code Playgroud)
但多线串仅适用于C++ 11,而#include不会没有多行字符串内工作(这是很好的)?
如果gcc有一个不可移植的方式......那将是一个开始.
澄清(更新):替换应在编译时完成.
我发现Peter Sommerlads Slides(第32页)中提到的零法则非常引人注目.
虽然,我好像记得,有一个严格的规则,一个有定义虚拟析构函数,如果类有虚拟成员和实际的.
struct Base {
virtual void drawYourself();
virtual ~Base() {}
};
struct Derived : public Base {
virtual void drawYourself();
};
Run Code Online (Sandbox Code Playgroud)
析构函数的主体甚至可能是空的(它只需要vtbl中的条目).
我似乎记得在使用层次结构时
int main() {
Base *obj = new Derived{};
obj->drawYourself(); // virtual call to Derived::drawYourself()
delete obj; // Derived::~Derived() _must_ be called
}
Run Code Online (Sandbox Code Playgroud)
那么delete obj 调用正确的析构函数是很重要的.这是正确的,如果我完全省略了析构函数定义,它就不会变成虚拟的,因此会调用错误的数字吗?
struct Base {
virtual void drawYourself();
// no virtual destructor!
}; …Run Code Online (Sandbox Code Playgroud) 我打电话时遇到异常metafactory.它说:
java.lang.invoke.LambdaConversionException:
Incorrect number of parameters for instance method
invokeVirtual my.ExecuteTest$AProcess.step_1:()Boolean;
0 captured parameters,
0 functional interface method parameters,
0 implementation parameters
Run Code Online (Sandbox Code Playgroud)
我不明白所有的文件LambdaMetafactory.metafactory.我在确定正确的参数时遇到了问题:
所以它归结为以下两者之间的区别:
我的代码是这样的:
package my;
import java.lang.invoke.*;
import java.lang.reflect.Method;
public class Execute {
public interface ProcessBase {};
@FunctionalInterface
public interface Step {
Boolean apply(); …Run Code Online (Sandbox Code Playgroud) 它是GCC 4.7.0还是我?我做错了什么?
这会引发std::system_error "不允许操作"的异常:
struct DumbFib {
size_t operator()(size_t n) { return fib(n); }
static size_t fib(size_t n) {
return n<2 ? 1 : fib(n-2)+fib(n-1);
}
};
void sample() {
DumbFib dumbfib;
thread th{ dumbfib, 35 }; // <- system_error!
th.join();
};
Run Code Online (Sandbox Code Playgroud)
虽然这有效:
void work(size_t loop) {
for(int l = loop; l>0; --l) {
for(int i = 1000*1000; i>0; --i)
;
cerr << l << "...";
}
cerr << endl;
}
int main() {
//sample();
thread t …Run Code Online (Sandbox Code Playgroud) 在这个关于期货,承诺和打包任务的优秀教程之后,我达到了我想要准备自己的任务的地步
#include <iostream>
#include <future>
using namespace std;
int ackermann(int m, int n) { // might take a while
if(m==0) return n+1;
if(n==0) return ackermann(m-1,1);
return ackermann(m-1, ackermann(m, n-1));
}
int main () {
packaged_task<int(int,int)> task1 { &ackermann, 3, 11 }; // <- error
auto f1 = task1.get_future();
thread th1 { move(task1) }; // call
cout << " ack(3,11):" << f1.get() << endl;
th1.join();
}
Run Code Online (Sandbox Code Playgroud)
至于我可以解密gcc-4.7.0错误消息,它期望参数不同吗?但是怎么样?我尝试缩短错误消息:
error: no matching function for …Run Code Online (Sandbox Code Playgroud) 我只写了一个简单的JUnit Matcher的assertThat()要求仿制药,当然.
通过一点点运气,我发现了正确的语法返回类型的static <T>Matcher not(Matcher<T> m)...,虽然我不明白为什么
<T>Matcher和Matcher<T>为什么它<T>Matcher在返回类型?这背后的概念是什么?
我来自C++,可以很好地处理它的模板.我知道Generics的工作方式不同,但这就是为什么这让我感到困惑.
这是我自己的Matcher课.看一下静态助手not:
import org.hamcrest.*;
/** assertThat(result, not(hasItem("Something"))); */
class NotMatcher<T> extends BaseMatcher<T> {
/** construction helper factory */
static <T>Matcher not(Matcher<T> m) { //< '<T>Matcher' ???
return new NotMatcher<T>(m);
}
/** constructor */
NotMatcher(Matcher<T> m) { /* ... */ }
/* ... more methods …Run Code Online (Sandbox Code Playgroud) 略过C++ 14/C++ 1y(n3690)的草案,我注意到在第21.7节中引入了basic_stringlitertal后缀:
inline namespace literals {
inline namespace string_literals {
// 21.7, suffix for basic_string literals:
string operator "" s(const char *str, size_t len);
u16string operator "" s(const char16_t *str, size_t len);
u32string operator "" s(const char32_t *str, size_t len);
wstring operator "" s(const wchar_t *str, size_t len);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
basic_string文字在运行时是否有可能更快?basic_string文字不同,或者在编译时与运行时的任何其他差异?我知道这允许直接使用这样的字符串文字:
std::string s1 = "A fabulous string"s;
void sfunc(std::string arg);
int main() {
sfunc("argument"s);
}
Run Code Online (Sandbox Code Playgroud)
但是,依赖转换构造函数 的优点是什么 …
我按照说明在我的页面中添加了Google环聊按钮:
<script src="https://apis.google.com/js/platform.js" async defer>
</script>
<g:hangout
render="createhangout"
topic="cpp11"
hangout_type="moderated"
invites="[{ id : '74838920', invite_type : 'PROFILE' },
{ id : 'my.email@gmail.com', invite_type : 'EMAIL' }]">
</g:hangout>
Run Code Online (Sandbox Code Playgroud)
当我按下该按钮时,视频环聊开始(告诉我我应该启用视频).我也不确定我是否有正确的环聊被邀请者,但我会稍后再说.
如何让按钮上的仅聊天窗口打开?
c++ ×5
c++11 ×4
java ×2
c ×1
c++14 ×1
destructor ×1
future ×1
gcc4 ×1
generics ×1
google-api ×1
hamcrest ×1
hangout ×1
inlining ×1
java-8 ×1
lambda ×1
literals ×1
optimization ×1
reflection ×1
return-type ×1
rule-of-zero ×1
stl ×1
string ×1
syntax ×1