C++ 将模板类型传递给函数并用于局部变量赋值c++;

C++ 将模板类型传递给函数并用于局部变量赋值c++;,c++,templates,assignment-operator,template-function,C++,Templates,Assignment Operator,Template Function,我有以下代码: template<typename T> void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen) { int nD = 0; // if T is of type std::pair<int,int> then set no. of dimensions to 2 if (typeid(T)

我有以下代码:

template<typename T> void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen) {

    int nD = 0;

    // if T is of type std::pair<int,int> then set no. of dimensions to 2
    if (typeid(T) == typeid(std::pair<int, int>)) {
        nD = 2;
    }

    // else if T is of type RWM::Triple<int,int,int> then set no. of dimensions to 3
    else if (typeid(T) == typeid(RandomWalkMethods::Triple<int, int, int>)) {
        nD = 3;
    }

    else {
        return;
    }

    // Create vector of T structs to store DLA structure results
    std::vector<T> aggResults;

    // Initialise particle spawning type and attractor type for DLA system
    RandomWalkMethods::ParticleSpawnType spawn = RandomWalkMethods::CONSTANT_RANDOM_BOUNDINGBOX_EDGE;
    RandomWalkMethods::AttractorDLAType attractor = RandomWalkMethods::POINT;

    // Under-estimate for fractal dimension of the DLA
    const double fractalDimUnderestimateRecip = 1 / 1.65;

    for (int i = 100; i <= 1000; i += 100) {

        // initialise spawnDiameter using: exp(log(n)/fDUR) = n^{1/fDUR}
        int spawnDiam = 2*static_cast<int>(std::pow(i, fractalDimUnderestimateRecip));

        // if system is 2-dimensional, compute DLA for 2D on given lattice
        if (nD == 2) {
            aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
        }

        // else if system is 3 dimensional, compute DLA for 3D on given lattice
        else if (nD == 3) {
            aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
        }

        // compute the minimum bounding radius which encloses all particles in the DLA structure
        double boundingRadius = std::sqrt(maxMagnitudeVectorOfMultiples< double, T >(aggResults));

    }


}
对于
Triple
的情况也是如此。据我所知,这意味着我需要为这两个结构使用重载赋值运算符,但我不认为这是问题所在,因为在我的程序中以前正确使用过以下语句:

std::vector< std::pair<int,int> > aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(nParticles, boundingBox, spawnDiam, latticeType, randNumGen, attractor, &diffLimAggFile);
std::vectoraggrestults=RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(nParticle、boundingBox、spawnDiam、latticeType、randNumGen、attractor和diffLimAggFile);
因此,我知道我可以将DLA方法的结果分配给正确类型的变量,但是如果我尝试使用如上所示的将类型传递给模板函数,编译器会抱怨


这里发生了什么?我将如何着手解决这个问题?

这是因为

aggResults = diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
即使
T
Triple
diffusionLimitedAggregateRandomWalk2D
返回
std::vector,也会编译
attracults


建议的解决方案:声明一个模板化函数,并为一些
T
专门化它

template<typename T>
void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen);

template<>
void computeFractalDimensionData<std::pair<int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}

template<>
void computeFractalDimensionData<Triple<int, int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}
模板
void computeFractalDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen);
模板
void ComputeFractDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen)
{
// ...
}
模板
void ComputeFractDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen)
{
// ...
}
它使代码更具可读性,无法编译以下代码行,并导致编译错误:

computeFractalDimensionData<void>(lattice, randNumGen);
computeFractalDimensionData(lattice,randNumGen);

这是因为

aggResults = diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
即使
T
Triple
diffusionLimitedAggregateRandomWalk2D
返回
std::vector,也会编译
attracults


建议的解决方案:声明一个模板化函数,并为一些
T
专门化它

template<typename T>
void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen);

template<>
void computeFractalDimensionData<std::pair<int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}

template<>
void computeFractalDimensionData<Triple<int, int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}
模板
void computeFractalDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen);
模板
void ComputeFractDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen)
{
// ...
}
模板
void ComputeFractDimensionData(RandomWalkMethods::LatticeType LatticeType,gsl_rng*randNumGen)
{
// ...
}
它使代码更具可读性,无法编译以下代码行,并导致编译错误:

computeFractalDimensionData<void>(lattice, randNumGen);
computeFractalDimensionData(lattice,randNumGen);

YSC的解决方案很好。我希望您注意,函数中的以下代码是对模板的错误使用:

    // if system is 2-dimensional, compute DLA for 2D on given lattice
    if (nD == 2) {
        aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
    }

    // else if system is 3 dimensional, compute DLA for 3D on given lattice
    else if (nD == 3) {
        aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
    }

模板用于静态多态性,并且在模板函数中使用动态代码(这些
if(nd==…)
)。正确使用静态多态性可以引入一个模板参数
维度

YSC的解决方案是好的。我希望您注意,函数中的以下代码是对模板的错误使用:

    // if system is 2-dimensional, compute DLA for 2D on given lattice
    if (nD == 2) {
        aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
    }

    // else if system is 3 dimensional, compute DLA for 3D on given lattice
    else if (nD == 3) {
        aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
    }

模板用于静态多态性,并且在模板函数中使用动态代码(这些
if(nd==…)
)。正确使用静态多态性可以引入模板参数
维度

@AngelusMortis What。我见过更长的。问题。尤其是更不可读的。您可以使用
std::tuple
而不是自制的Tripple和std::pair:。请查看helper类
tuple\u size
@和yt。不幸的是,此代码必须在非常旧的服务器上运行,该服务器仅具有C++98兼容性,因此
std::tuple
对我不可用:(@AngelusMortis What.我看到了更长的问题。尤其是更难阅读的问题。你可以使用
std::tuple
而不是自制的triple和std::pair:。看看helper类
tuple\u size
,不幸的是,这段代码必须运行在一个非常旧的服务器上,它只有C++98兼容性,因此
std::tuple
对我不可用:(