just returning non type parameter

bikashamit

I just learned compile-time array declaration via template, with the non-type parameter. so in this approach, we pass constant and array with size gets declared. like

#include<iostream>
using namespace std;

template<int size_of_array>
class arr 
{
public:
    int  arr[size_of_array];

    int size_of() { return size_of_array;   }
};

int main() 
{
    arr<4> arr1;
    cout << arr1.size_of();

    return 0;
}

Can we do something like variable value assigning at run time like if we remove arr[] from there. like

#include<iostream>
using namespace std;

template<int size_of_array>
class arr 
{
public:
    int  size_of_array;
    int size_of() { return size_of_array;   }
};

int main() 
{
    arr<4> arr1;
    cout << arr1.size_of();

    return 0;
}

Can we do that? or why not? Then maybe there is some thing about an array and a variable declaration which i don't know. Thank you in advance.

Lightness Races in Orbit

Absolutely! You can expose the template parameters by writing a function to pass them through.

Your attempt only failed because:

  • you tried to re-use the template argument name for a data member name, and
  • you never actually assigned a value to the data member.

There's really no need for an additional data member here anyway, as the template argument is accessible throughout the class definition.

So, for a non-type template parameter like yours, just:

template <int size_of_array>
class arr 
{
public:
    static int size_of() { return size_of_array; }
};

Now arr<42>::size_of() is 42!

(I've made it static, not because you need to, but because in this example it makes sense; you could alternatively make it a non-static but const member function.)

And for a type:

template <typename T>
class arr 
{
public:
    using array_size_t = T;
};

Now arr<T>::array_size_t is the type T!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Returning a type depending on the parameter

Non type template parameter

Passing function with parameter, as a parameter and returning a generic type

Passing just a type as a parameter in C#

Non Deduced context for a non type parameter

Implement interface with method returning class of type parameter

Returning a value depending on the type of the template parameter

Type No return, in function returning non-void

Method type returning an object using this.type as a type parameter

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

Returning generic type which is a specific collection based on type passed to parameter

How to look up the type of a method returning a type parameter with scala reflection?

Returning type based on a string parameter used as keyof a Record type

non type template parameter pack expansion

pointer non-type template parameter

Partial specialization of non-type parameter

Non-type variadic template parameter

Why is it called "non-type" template parameter?

Non-type reference parameter/argument

Template function dependent on non-type parameter

Template non-type parameter deduction

Template non-type parameter with different types

template non-type template parameter

Passing non-type parameter agument for templates

Integer sequence as a non-type template parameter

Alias Template with non-type template parameter

Calling a fun with reified type parameter if you just have a KClass object?

Specialize template template parameter with a non-type template parameter

Preserving type signature when returning a function that was supplied as a parameter

TOP Ranking

HotTag

Archive