Type definitions dependent on template parameters

quantenstau

I am currently working with template classes in C++. In these classes, I am using types that are again dependent on the template parameters. In order to not type the parameters all the time, I did something along the lines of

template<typename T>
class A
{
    using someName = someClass<T>;
}

There are some more examples in the actual code but this should illustrate the idea.

This worked fine, as long as I only needed someName in class A. But my project has since grown and I have added other templated classes that should use someName as well.

My question is: What is the best way to define (several) types as above that depend on some template parameters and use them in multiple other classes (at best without having the write out all of the parameters constantly)?

My ideas so far:

  1. Of course, I can simply copy using someName = std::someClass<T>; to all the classes using it. But this is not very elegant and both adding classes and adding such types becomes increasingly cumbersome.
  2. An alternative would be to write all the usings into a file and let the compiler do the copy & pasting for me via include. But I am not sure whether this would actually work and it seems to be a very brute force approach and in general not good practice.
  3. I also tried different approaches involving using but none of them worked so far. Still, it seems to me that this might be the most promising route to the solution.

Maybe I am making things way too complicated and there is an easier and more straightforward solution to this.

passing_through

Create a struct which defines each common alias:

template<typename T> struct Aliases {
    using value_type = T;
    using reference = T&;
};

...and inherit from it:

template<typename T> class Foo: public Aliases<T> {};
template<typename T> class Bar: public Aliases<T> {};
template<typename T> class Baz: public Aliases<T> {};

Some tests:

static_assert(std::same_as<Foo<char>::value_type, char>);
static_assert(std::same_as<Bar<int>::reference, int&>);
static_assert(std::same_as<Baz<int>::reference, int&>);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type definition should be dependent on template parameters

How to define a type dependent on template parameters

Is a function type dependent if it depends only on its own template parameters?

Type dependent template name

Use template from dependent type

Function template overload resolution, dependent and non-dependent parameters

deduce type of variable dependent on template function type

c++ Tightly Coupled Template Definitions, Infinitely Recurring Template Parameters

Type-dependent constant in template function

Template function dependent on non-type parameter

How to specialize template on arbitrary dependent type

Type trait dependent specialization of template function

Non-type template parameter dependent on a default template type parameter

Expressions depending on integer type parameters in type definitions in Julia are not allowed

Will consteval functions allow template parameters dependent on function arguments?

template parameters in templated type function

Nested template parameters and type deduction

template non-type template parameters

Indexing a nested object with two dependent type parameters fails in Typescript 3.9

Co-Dependent Definitions in C

Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

Why can't I specialize with a nullptr template parameter on a dependent type?

Is a c++11 variadic function template overload with a dependent type ambiguous?

Overloading operator "<<" in C++ with template-dependent type

Unify type and non-type template parameters

How can a type alias with using specify a template template argument dependent on a template argument?

Can a template template parameter default reference other template type parameters?

Template template parameter with mixed type and non-type variadic parameters

Partial specialization of single type template parameter class template using non-type template parameter through a dependent type

TOP Ranking

HotTag

Archive