如何为也可用于友元函数声明的模板函数创建 typedef 或别名?

How can I make a typedef or alias for a template function that can also be used in friend function declarations?

如果我有这样的东西:

mytemplates.h

#ifndef MYTEMPLATES_H
#define MYTEMPLATES_H

#include <iostream>
#include <memory>
#include <string>

class BaseClass;
// Alias (non-working) version:
//class DerivedClass1;

namespace MyTemplates
{
    typedef std::unique_ptr<BaseClass> object_up;
    
    template<typename SomeDerivedClass>
    object_up createFromIntFloat(int intParam, float floatParam)
    {
        SomeDerivedClass *newObject_raw = new SomeDerivedClass(intParam, floatParam);

        object_up uniqueObject {newObject_raw};

        std::cout << "createFromIntFloat." << "\n";

        return uniqueObject;
    }
    // Alias (non-working) version:
    //using createFromIntFloatPtrType = object_up(*)(int intParam, float floatParam);

    template<typename SomeDerivedClass>
    object_up createFromString(std::string stringParam)
    {
        SomeDerivedClass *newObject_raw = new SomeDerivedClass(stringParam);

        object_up uniqueObject {newObject_raw};

        std::cout << "createFromString." << "\n";

        return uniqueObject;
    }
    // Alias (non-working) version:
    //using createFromStringPtrType = object_up(*)(std::string stringParam);  

    // Alias (non-working) version:
    // createFromStringPtrType createDerivedClass1 =  createFromString<DerivedClass1>;
}

#endif // MYTEMPLATES_H

baseclass.h

#ifndef BASECLASS_H
#define BASECLASS_H

class BaseClass
{
protected:
    BaseClass(){};
};

#endif // BASECLASS_H

derivedclass1.h

#ifndef DERIVEDCLASS1_H
#define DERIVEDCLASS1_H

#include "baseclass.h"
#include "mytemplates.h"

class DerivedClass1 : public BaseClass
{
    std::string stringParam;
protected:
    DerivedClass1(std::string aStringParam) : BaseClass(), stringParam(aStringParam) {};

    // Alias (non-working) version:
    // friend MyTemplates::object_up MyTemplates::createDerivedClass1(std::string stringParam);

    // Non-alias version:
    friend MyTemplates::object_up MyTemplates::createFromString<DerivedClass1>(std::string stringParam);
};

#endif // DERIVEDCLASS1_H

main.cpp

#include "mytemplates.h"
#include "derivedclass1.h"

int main(int argc, char *argv[])
{
    // Alias (non-working) version:
    // MyTemplates::object_up uniqueClass1 = MyTemplates::createDerivedClass1("creation info for class 1");
    
    // Non-alias version:
    MyTemplates::object_up uniqueClass1 = MyTemplates::createFromString<DerivedClass1>("creation info for class 1");
}

我想在上面的代码中创建某种别名,例如 Alias(非工作)版本,以便我可以使用这个别名 - createDerivedClass1 - 在 main.cpp 中的直接调用和 derivedclass1.h[ 中的友元声明中=14=]

如何将上面的代码从 非别名版本 更改为 别名版本 并使其正常工作?

别名用于类型,因为类型不是 C++ 中的第一个 class 对象。但是你不需要普通函数的别名,因为你可以简单地使用函数指针。

但是函数指针只是一个指针,您必须将真正的函数声明为友元。这一切都归结为:

...
class DerivedClass1 : public BaseClass
{
    std::string stringParam;
protected:
    DerivedClass1(std::string aStringParam) : BaseClass(), stringParam(aStringParam) {};

    // Alias (non-working) version:
    //friend MyTemplates::object_up MyTemplates::createDerivedClass1(std::string stringParam);

    // Non-alias version:
    friend MyTemplates::object_up MyTemplates::createFromString<DerivedClass1>(std::string stringParam);
};

namespace MyTemplates {
    object_up(*createDerivedClass1)(std::string) = createFromString<DerivedClass1>;
}

int main(int argc, char* argv[])
{
    // Alias (non-working) version:
    MyTemplates::object_up uniqueClass1 = MyTemplates::createDerivedClass1("creation info for class 1");

    // Non-alias version:
    MyTemplates::object_up uniqueClass2 = MyTemplates::createFromString<DerivedClass1>("creation info for class 1");
}