我有一个函数,它采用指向浮点数组的指针.基于其他条件,我知道指针实际上指向2x2 OR 3x3矩阵.(实际上内存最初是这样分配的,例如float M [2] [2])重要的是我想在函数体中做出这个决定,而不是作为函数参数.
void calcMatrix( int face, float * matrixReturnAsArray )
{
// Here, I would much rather work in natural matrix notation
if( is2x2 )
{
// ### cast matrixReturnAsArray to somethingAsMatrix[2][2]
somethingAsMatrix[0][1] = 2.002;
// etc..
}
else if(is3x3)
{ //etc...
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用模板和其他技术来更好地解决这个问题.我的问题是关于如何在###评论中进行这样的演员表.使用C++.
如果模板类定义包含依赖于模板类型的静态成员变量,我不确定应该采用什么样的可靠行为?
在我的例子中,最好将静态成员的定义放在与类定义相同的.h文件中,因为
MyClass<int>,一个为所有MyClass<double>,等等.我可以最简单地说,在使用gcc 4.3编译时,此链接中列出的代码完全符合我的要求.这种行为是否符合C++标准,以便在使用其他编译器时可以依赖它?
该链接不是我的代码,而是CodeMedic在此处讨论的一个反例.我发现其他几个辩论这样的一个,但没有,我认为定论.
我认为链接器正在整合找到的多个定义(在示例a.o和中b.o ).这是必需/可靠的链接器行为吗?
与虚拟成员函数相反,我需要一种解决方案,其中可以注册在每个级别类派生中实现的函数,以便稍后由基类调用.(不仅仅是最衍生的实现)
为此,我正在考虑为派生类提供一种机制,以便在基类中注册它们的函数,例如在派生类构造函数中.
我遇到了成员函数指针参数的问题.我以为Derived是从Base派生的,this指针应该自动生成.
这可以接近我正在尝试或我需要使用静态成员函数void *,和static_cast?
class Base
{
protected:
typedef void (Base::*PrepFn)( int n );
void registerPrepFn( PrepFn fn ) {};
}
class Derived : public Base
{
Derived() {
registerPrepFn( &Derived::derivedPrepFn );
};
void derivedPrepFn( int n ) {};
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error: no matching function for call to 'Derived::registerPrepFn(void (Derived::*)(int))'
note: candidates are: 'void Base::registerPrepFn(void (Base::*)(int))'
Run Code Online (Sandbox Code Playgroud) 好的,我正在使用虚函数,重载函数和多重继承.当然这并不好.
场景:Class base1有一个需要由其子节点指定的虚函数.类derived两个亲本的派生base1和base2,并应使用base2现有的功能来定义base1的虚函数.
这没关系,但当然很尴尬.动机是我无法改变类base2和现有的接口已经大量投入了同名base1和base2类功能.这没关系,没有实现base1,它只应重定向到base2.
我的问题出现了,因为base2有几个与所讨论的虚函数同名的重载函数.所有其他重载版本在编译时基本上都是隐藏的.
这是一个小的演示代码.
// this version does not compile, overloaded samenameFunc(int, int) cannot be found in the derived class.
#include <iostream>
using namespace std;
class base1 {
public:
virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};
class base2 {
public:
void samenameFunc(int scratch) { cout << …Run Code Online (Sandbox Code Playgroud) 我有一个二维数据和四边形的二维网格描述一个细分为补丁的域.数据在每个网格节点处定义.数据的不连续性存在于补丁边界处,即数据在同一位置被多次定义.
如何使用Python在节点之间使用线性插值绘制此数据,并在每个补丁面上正确表示不连续值?
下面是三个示例元素或补丁,每个元素或补丁各有六个节点值.

节点位置和值数据可以存储在[Kx3x2]数组中,其中K是元素的数量.例如,
x = np.array( [
[ [0.0, 1.0], [0.0, 1.0], [0.0, 1.0] ], #element 0
[ [1.0, 2.0], [1.0, 2.0], [1.0, 2.0] ], #element 1
[ [2.0, 3.0], [2.0, 3.0], [2.0, 3.0] ], #element 2
] )
y = np.array( [
[ [0.0, 0.0], [0.5, 0.5], [1.0, 1.0] ], #element 0
[ [0.0, 1.0], [0.5, 1.5], [1.0, 2.0] ], #element 1
[ [1.0, 1.0], [1.5, 1.5], [2.0, 2.0] ], #element 2
] )
z = …Run Code Online (Sandbox Code Playgroud) 给定一个共享的整数计数器数组,我想知道线程是否可以在不锁定整个数组的情况下自动获取和添加数组元素?
这是使用互斥锁锁定对整个数组的访问的工作模型的说明。
// thread-shared class members
std::mutex count_array_mutex_;
std::vector<int> counter_array_( 100ish );
// Thread critical section
int counter_index = ... // unpredictable index
int current_count;
{
std::lock_guard<std::mutex> lock(count_array_mutex_);
current_count = counter_array_[counter_index] ++;
}
// ... do stuff using current_count.
Run Code Online (Sandbox Code Playgroud)
我希望多个线程能够同时获取添加单独的数组元素。
到目前为止,在我对std::atomic<int>I 的研究中,构造原子对象也构造了受保护的成员。(还有很多答案解释了为什么你不能做一个std::vector<std::atomic<int> >)
我正在尝试创建一个扩展boost图库行为的类.我希望我的类成为一个模板,用户提供一个类型(类),用于存储每个顶点的属性.那只是背景.我正在努力创建一个更简洁的typedef来用于定义我的新类.
基于其他职位喜欢这个和这个,我决定来定义一个struct将包含模板的typedef.
我将展示两种密切相关的方法.我无法弄清楚为什么GraphType的第一个typedef似乎正在工作,而VertexType的第二个类型失败.
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
template <class VP>
struct GraphTypes
{
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType;
typedef boost::graph_traits< GraphType >::vertex_descriptor VertexType;
};
int main()
{
GraphTypes<int>::GraphType aGraphInstance;
GraphTypes<int>::VertexType aVertexInstance;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
$ g++ -I/Developer/boost graph_typedef.cpp
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’
graph_typedef.cpp: In function ‘int main()’:
graph_typedef.cpp:14: error: ‘VertexType’ is not a member …Run Code Online (Sandbox Code Playgroud) 通过不幸的情况,我发现我的标准库实现 <math.h>和<cmath>(C ++)显然包含具有原型的函数的定义,例如:
double gamma(double x);
Run Code Online (Sandbox Code Playgroud)
虽然我没有在语言标准的任何地方看到它(我可以使用草稿)。
在Mac OS X上使用gcc v4.2.1,该函数的评估结果与tgamma标准中实际为其指定的名称相同。(参考)
但是在Ubuntu 12.04上的gcc v4.6.3上,该函数的计算结果有所不同。
我根本不理解为什么一个名为函数的函数都可以gamma编译,但是为什么编译器之间的结果不一样?
这是一个简单的测试程序:
#include<iostream>
#include<sstream>
#include<math.h> // or <cmath>
int main(int argc, char **argv)
{
std::stringstream conversionStream(argv[1]);
double input;
conversionStream >> input;
std::cout << "gamma( " << input << " ) = " << gamma(input) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并使用1个参数运行:
$ g++ -o gamma_test gamma_test.cpp
$ ./gamma_test 1.0
gamma( 1 ) = 1
Run Code Online (Sandbox Code Playgroud)
但是Ubuntu gcc v4.6.3的输出为0!
这个问题关系到这一个除了不是处理类型名称的模板参数,我想使用一个枚举非类型模板参数.
是否有可能只有特化的模板化(类成员函数),在非类型模板参数的情况下没有通用(工作)定义?
通过在类体中声明并仅提供特化,我能够使一个版本工作,但是任何使用非定义模板参数的误操作都不会产生错误,直到链接.更糟糕的是,缺失的符号隐含地指的是枚举的整数值而不是它的名称,所以它会让其他开发人员感到困惑.
我能够BOOST_STATIC_ASSERT从引用的问题中获取技术仅适用于typename模板参数.
此代码演示了这个想法.我不希望CAT-version调用编译:
#include <iostream>
#include <boost/static_assert.hpp>
// CLASS HEADER FILE:
struct foo_class
{
enum AllowedTypes { DOG, CAT };
template <AllowedTypes type>
void add_one_third( double bar ) const
{
BOOST_STATIC_ASSERT_MSG(sizeof(type)==0, "enum type not supported.");
}
};
// CLASS SOURCE FILE
template<>
void foo_class::add_one_third<foo_class::DOG>( double bar ) const
{
std::cout << "DOG specialization: " << bar + 1./3. << std::endl;
}
// USER SOURCE FILE
int main()
{
std::cout << "Template Specialization!\n\n"; …Run Code Online (Sandbox Code Playgroud)