我想T1,T2在创建类的实例时省略一些模板参数DeriveGenerator<T3,T4,T1,T2>来安慰我的生活.
这是我遇到的最终简化版本.
我的图书馆:-
重要的部分是阶级宣言.(this line)
他们的内部内容只是一个填充物.
template<class T1,class T2>class BaseGenerator{ //<-- this line
public: std::pair<T1*,T2*> generateBase(){
/** actually create T3,T4 internally */
return std::pair<T1*,T2*>(nullptr,nullptr);
}
};
template<class T3,class T4,class T1,class T2>class DeriveGenerator{ //<-- this line
public: Base<T1,T2>* base;
public: std::pair<T3*,T4*> generateDerive(){
auto pp=base->generateBase();
return std::pair<T3*,T4*>((T3*)(pp.first),(T4*)(pp.second));
}
};
Run Code Online (Sandbox Code Playgroud)
用户:-
class B1{};class B2{};
class B3:public B1{};
class B4:public B2{};
int main() {
//v this is what I have to
BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4,B1,B2> deriveGen; //so dirty #1 …Run Code Online (Sandbox Code Playgroud) 用户想传递副本,但是被封装库阻止了[&],这是coliru MCVE:-
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>
#include <functional>
int main(){
std::vector<std::function<void()>> funcs;
for(int n=0;n<3;n++){
auto func=[&,n](){ //[=] for n
std::cout<<""<<n; // user's code
};
//v library (actually inside another utility function)
funcs.push_back([&](){ //user's "n" is blocked ??
//(some library-related code here)
func();
});
//^ library
}
//v library
for(int m=0;m<3;m++){
funcs[m]();
}
}
Run Code Online (Sandbox Code Playgroud)
它打印222而不是012。
为什么,以及如何解决呢?
请注意,该库无法了解n。
根据一个相关的问题(按值捕获c ++ lambda),应正确复制该值。
这是一个做同样事情的更复杂的MCVE,但是可以更好地描述我实际使用它的方式:-
#include <iostream> …Run Code Online (Sandbox Code Playgroud) 我刚发现间接成本大约是浮点乘法的3倍!
这是预期的吗?我的考试错了吗?
在我读了指针间接对效率有多大影响?,我对间接成本感到恐慌.
由于现代CPU的工作原理,通过指针间接可能要慢得多.
在我过早优化我的真实代码之前,我想确保它真的花费很多,因为我担心.
我做了一些技巧来找到粗略数字(3x),如下所示: -
我发现Test2需要更多的时间来测试Test1.
这里没什么好惊讶的.
我尝试将我的代码更改calculate something expensive为更加昂贵,以使两个测试成本接近相同.
最后,我发现使两个测试使用相同的时间(即收支平衡)的可能功能之一是: -
float*float*...3次 float 这是我的测试用例(ideone演示): -
class C{
public: float hello;
public: float hello2s[10];
public: C(){
hello=((double) rand() / (RAND_MAX))*10;
for(int n=0;n<10;n++){
hello2s[n]= ((double) rand() / (RAND_MAX))*10; …Run Code Online (Sandbox Code Playgroud) 这是示例代码:-
unsigned int instanceVBO;
glGenBuffers(1, &instanceVBO);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBindVertexArray(VAO);
...
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 100,
&translations[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
... ... ...
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(2, 1); //<--- who own this setting?
//^ mostly copied from https://learnopengl.com/Advanced-OpenGL/Instancing
Run Code Online (Sandbox Code Playgroud)
谁拥有该glVertexAttribDivisor设置?(VAO /实例VBO /全局状态)
https://gamedev.stackexchange.com/questions/99236/what-state-is-stored-in-an-opengl-vertex-array-object-vao-and-how-do-i-use-the中的评论#comment174555_99238 表明它存储在VBO中。
但是,该注释与上面在unbindsglVertexAttribDivisor(2, 1) 之后glBindBuffer(GL_ARRAY_BUFFER, 0);调用的代码相矛盾(?) 。
如果您也愿意提供参考,我将不胜感激,我可以阅读更多有关:
哪些设置/状态由 Opengl 的哪一个东西(VAO/VBO/等)拥有。
一个解决方案SolA仅包含1个名为的项目PrjA。
PrjA是Win32控制台应用程序,编译结果为PrjA.exe。
如何将源代码文件拆分PrjA为PrjA1和PrjB,以便源代码的管理更加轻松,而编译结果PrjA1.exe与几乎相同PrjA.exe?
例如,我有PrjA:-
PrjA有300个.cpp文件。PrjA.exe 大小400KB。 我希望PrjA分为两个项目:
PrjA1有200个.cpp文件 PrjB有100个.cpp文件。PrjA1.exe 也约为400KB。 我不确定如何设置PrjB或它的编译结果是什么。
假设PrjB编译为PrjB.DLL100KB,大小,希望PrjA1.exe以某种方式嵌入其PrjB.dll内部。因此,大小为400KB。
我不需要300KB PrjA1.exe,它将PrjB.dll在运行时动态链接到100KB 。
问:我应如何设置PrjB,我应如何设置之间的联系PrjA1和PrjB?
与方案1相同,只是这一轮PrjA的编译结果是一个Windows DLL,名为PrjA.dll,该如何拆分PrjA为PrjA1和 …
我正在学习如何获得type重载函数test()vs 的返回值test(double)。
我从SO答案(由chris)修改了代码。
#include <type_traits>
#include <utility>
int test();
double test(double x);
template<typename... Ts>
using TestType = decltype(test(std::declval<Ts>()...))(Ts...);
int main() {
std::result_of< TestType<double> >::type n = 0;
//^ ### compile error ###
using doubleDat = std::result_of< TestType<double> >::type ;
doubleDat n=0;
}
Run Code Online (Sandbox Code Playgroud)
我遇到了编译错误。
错误:“ std :: result_of”中没有名为“ type”的类型
我认为:-
TestType<...>是“可变模板”。
用我自己的话说,它就像一个带有任何参数的压缩缩写。
该TestType<double>是ID的的test(double)功能。
std::result_of<TestType<double>>::type是的返回类型test(double)。doubleDat应该是double。问题: …
是否有可能void*保留类型信息?
我试图使它忘记了真正的类型(B*)通过投B* b来void* v,但它似乎知道它的起源.
这对我来说是好运,但我不知道为什么.
这是一个简化的代码(完整演示): -
#include <iostream>
#include <memory>
using namespace std;
class B{public: int ib=1;};
class C:public B{public: int ic=2;};
int main() {
B* b=new C();
void* v=b; //<- now you forget it!
C* c=static_cast<C*>(v);
std::cout<<c->ib<<" "<<c->ic<<std::endl; //print 1 and 2 correct (it should not)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望c指向B*(一个错误的地址)的地址,但似乎知道这v是B*正确的.
它为什么有效?难道void*真的记得类型?
它能"记住"多远?
我试图在这里创建一个void* …
如何指定模板模板参数的最左侧模板参数?
B<int,T2>是一个带有2个参数的模板类.
E<H>是一个接收"模板模板参数"的类H<T2>.
我想通过B插入插槽H中E<H>,如:E<B<1>>,E<B<2>>,E<B<42>>等.
不过,我只找到了工作穷人周围,如别名B1<T>=B<1,T>然后传递B1到H-Slot: -
class D{}; //a library class
template<template<class T2>class H>class E{ //a library, don't modify me
//"H<T2>" is "B1<T2>" = "B<1,T2>"
H<D> h; //Here, T2=D, so "H<D>" = "B<1,D>"
};
template<int n,class T2> class B{}; //a user class
template<class T2> using B1 = B<1,T2>; //a user class
int main() {
E<B1> e; //I …Run Code Online (Sandbox Code Playgroud) 我想重新分配一个具有自引用的名为的类CarJoker。
“重新分配”在这里意味着=更改对象的地址。
要使每个CarJoker生命实例都处于可调整大小的连续数组(例如池)中,此技术是必需的。
我考虑使用std::move,但是它无法CarJoker::wheels按照我希望的方式移动。
(MCVE)
#include <vector>
#include <iostream>
#include <string>
struct Wheel{
void setBlade(){}
void setTwinkle(){}
};
struct CarJoker{
Wheel wa;
Wheel wb;
Wheel wc;
std::vector<Wheel*> wheels;
float hp=5;
CarJoker(){
wheels.push_back(&wa);
wheels.push_back(&wb);
wheels.push_back(&wc);
}
void wow(){
//v want to apply something to every "wheel"
for(auto wheel:wheels){
wheel->setBlade();
}
//v want to apply something to some certain "wheel"
wa.setTwinkle();
}
};
int main(){
CarJoker car1;
CarJoker car2=std::move(car1);
std::cout<<"want to 1 …Run Code Online (Sandbox Code Playgroud) c++ ×8
c++14 ×6
decltype ×2
templates ×2
c++11 ×1
indirection ×1
lambda ×1
move ×1
opengl ×1
optimization ×1
overloading ×1
raw-pointer ×1
result-of ×1
static-cast ×1