struct vec2
{
union
{
struct { float x, y; };
struct { float r, g; };
struct { float s, t; };
};
vec2() {}
vec2(float a, float b) : x(a), y(b) {}
};
struct vec3
{
union
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
// Here is the problem with g++.
struct { vec2 xy; float z; };
struct { float x; vec2 yz; };
};
vec3() {}
vec3(float a, float b, float c) : x(a), y(b), z(c) {}
};
上面的代码在 Visual Studio 中按预期编译和工作,所以我可以像使用它一样
vec3 v1(1.f, 2.f, 3.f);
vec2 v2 = v1.yz; // (2, 3)
不是在 g++ (MinGW) 中。
src/main.cpp:22:23: error: member 'vec2 vec3::<unnamed union>::<unnamed struct>::xy' with constructor not allowed in anonymous aggregate
src/main.cpp:22:33: error: redeclaration of 'float vec3::<unnamed union>::<unnamed struct>::z'
src/main.cpp:18:30: note: previous declaration 'float vec3::<unnamed union>::<unnamed struct>::z'
src/main.cpp:23:32: error: member 'vec2 vec3::<unnamed union>::<unnamed struct>::yz' with constructor not allowed in anonymous aggregate
src/main.cpp:23:24: error: redeclaration of 'float vec3::<unnamed union>::<unnamed struct>::x'
src/main.cpp:18:24: note: previous declaration 'float vec3::<unnamed union>::<unnamed struct>::x'
我想我一开始就不应该那样做。有什么想法吗?
编辑:在阅读了很多文章并探索了开源项目之后,我开始了解vector swizzling应该是什么样的,并在下面发布了解决方案,但仍在等待更好的答案。 p>
Edit 2: All
vec*members must be accessed only from the parent like the GLM library.
最佳答案
首先,匿名结构是a feature from C11 , 是 not allowed by C++ ,因此它不支持具有构造函数的类成员(不是 C 结构)。要编写可移植的 C++ 代码,您应该避免使用匿名结构:
struct vec2 // use C++ style struct declaration
{
// struct is public by default
union
{
struct { float x, y; } xy; // add member name,
struct { float r, g; } rg; // now the declaration declares a member
struct { float s, t; } st; // instead of an anonymous struct
};
vec2() {}
vec2(float a, float b) : xy{a, b} {}
// ^^^^^^^^ also change the initialization
};
struct vec3
{
public:
union
{
struct { float x, y, z; } xyz; //
struct { float r, g, b; } rgb; //
struct { float s, t, p; } stp; // add member name
struct { vec2 xy; float z; } vecz; //
struct { float x; vec2 yz; } xvec; //
};
vec3() {}
vec3(float a, float b, float c) : xyz{a, b, c} {}
// ^^^^^^^^ also change the initialization
};
现在代码可以在 GCC 下编译,但这还不够。在带有 -pedantic-errors 的 Clang 下,你会得到 several errors :
error: anonymous types declared in an anonymous union are an extension [-Werror,-Wnested-anon-types]
这是因为您不能在匿名 union 中声明嵌套类型,因此您还应该将这些结构定义移到 union 之外:
struct vec2
{
struct XY { float x, y; };
struct RG { float r, g; };
struct ST { float s, t; };
union
{
XY xy;
RG rg;
ST st;
};
vec2() {}
vec2(float a, float b) : xy{a, b} {}
};
struct vec3
{
struct XYZ { float x, y, z; };
struct RGB { float r, g, b; };
struct STP { float s, t, p; };
struct VECZ { vec2 xy; float z; };
struct XVEC { float x; vec2 yz; };
union
{
XYZ xyz;
RGB rgb;
STP stp;
VECZ vecz;
XVEC xvec;
};
vec3() {}
vec3(float a, float b, float c) : xyz{a, b, c} {}
};
尽管此解决方案有效,但您只能通过例如 v.xy.x 访问成员,而不是简单的 v.x。此外,使用两个 float 为 vec2 起别名会导致未定义的行为。我认为没有标准的解决方案可以完美地实现 vector 混合。
对于非标准解决方案,可以使用没有构造函数的代理类代替 vec2 来使编译器工作。 GLM library也使用了这个想法。 OP 已经发布了 answer作为这个想法的完整实现。
关于c++ - 如何在 C++ 中实现 vector 混合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641131/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121