0
template <typename T>
class myTemplateClass
{
public:
    myTemplateClass()
    {
        // do something
    }

    myTemplateClass(T t)
        : val{t}
    {
        // do something
    }
    T val;
};

class mySimpleClass
{
public:
    mySimpleClass()
    {
        // do something
    }
};

template <template<typename> class TT, typename T = void>
auto create()
{
    if constexpr (std::is_same_v<TT<T>, myTemplateClass<T>>)
    {
        // do something
        // and return
        return myTemplateClass<T>();
    }
    else
    {
        //constexpr (std::is_same_v<TT, mySimpleClass>)
        // do something
        // and return
        return  mySimpleClass{};
    }
};

int main()
{
    {
        auto result = create<myTemplateClass<int>>();
    }
    {
        auto result = create<mySimpleClass>();
    }

    return 0;
}

I tried to implement one create() function which return one of a template class and a normal class based on template parameter, but it has an error.

How can I implement create() to support this situation?

mostly, it is used as like

create<int>();
create<double>();

but if I want to mix as like create<myTemplateClass>() and create in one create function based one template function, is there any way?

yi bruce
  • 71
  • 4
  • `void main(char**, int)` isn't a valid signature for main. See [What is the proper declaration of main in C++?](https://stackoverflow.com/q/4207134/11082165) – Brian61354270 Jan 25 '23 at 02:17
  • You can't pass `mySimpleClass` to a template template argument. Also, since `TT` is a template template argument, `create>()` would need to be `create()` instead. Since you are passing two types to `create()`, not templates, what's wrong with simply using: `template T create() { return T{}; }`? Why are you trying to filter the types? – Remy Lebeau Jan 25 '23 at 02:22
  • if so, what is proper value for "??????" part in code 'if constexpr (std::is_same_v>)' – yi bruce Jan 25 '23 at 02:34
  • I just want to simple create function for creating instance of mySimpleClass or instance of myTemplateClass. – yi bruce Jan 25 '23 at 02:39

2 Answers2

1
template<typename> class TT

TT is a template.

std::is_same_v<TT, mySimpleClass>

Both parameters to std::is_same_v are types, and not templates. The first parameter specified, TT, is a template instead of a type. This is the reason for your compilation error.

It is unclear what was the intent here, so it's not possible to suggest, with reasonable certainty, what should this be changed to.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 1
    Likewise neither `create>` nor `create` make sense, since `myTemplateClass` and `mySimpleClass` are types, not templates. – Brian61354270 Jan 25 '23 at 02:22
  • if so, what value can I specify for "?????" part in 'if constexpr (std::is_same_v>' – yi bruce Jan 25 '23 at 02:33
  • I just want to create one simple create method as template which return mySimpleClass or return myTemplateClass – yi bruce Jan 25 '23 at 02:37
  • You need to provide a step by step specification that defines which template parameters result in one class, and which template paramers result in the other one. You might have them in your head, but we don't. – Sam Varshavchik Jan 25 '23 at 02:46
1

Mistake 1

First things first, the signature of main in your example is not correct. Ideally it should be int main()(instead of void main(char**, int).


Mistake 2

Moreover, myTemplateClass<int> is a class-type not a class-template.

One way to solve this would be:

template <typename Type>
auto create()
{
    return Type{};
};
Jason
  • 36,170
  • 5
  • 26
  • 60