Insert array data into template

EarlyEarl

I want to fill a template with some integers from an array but this doesn't seem to be allowed in C++. An example:

I define a constant array with Integers.

const int array[4] = {0, 1, 2, 3};

My template works like that:

template<int T> TestClass ...

In the following, the first way is no problem, but the second way does not compile:

TestClass<12> ...          // works
TestClass<array[0]> ...    // does not work

The compiler says that the operator '[' is not allowed in a temlate. But what is the reason for that and how can I solve this situation elegantly? Is it also possible to use a counter iof a for-loop to choose the Integer of the array, like:

TestClass<array[i]>
Guillaume Racicot

Turn your array to a constexpr variable:

constexpr int array[4] = {0, 1, 2, 3};

or even better:

constexpr std::array<int, 4> myArray{0, 1, 2, 3};

Since your array will be constexpr, it's usage will be valid at compile-time.

Be sure to enable c++14 for the std::array version.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

insert data into array

From Dev

Insert array data to database

From Dev

Insert data into array [Swift]

From Dev

How to insert data to a template after render ? (django)

From Dev

Insert array data in php/mysql

From Dev

Insert data into the file as an array in bash

From Dev

Insert data into a multidimensional array in php

From Dev

How to insert array of data into table

From Dev

insert data from array to DB

From Dev

Insert data in an array list java

From Dev

Unable to insert data from post array in MySQL

From Dev

mongoose : insert data into an array of nested objects

From Dev

Using Rails console to insert an array of data

From Dev

How to insert multidimensional array data in react js?

From Dev

How to insert all array data into mysql?

From Dev

How to insert data into MySql using an associative array

From Dev

Insert different types of data to array - Android

From Dev

Using Rails console to insert an array of data

From Dev

How to insert data as array values into MySQL?

From Dev

insert data in associative array based on condition PHP

From Dev

Laravel - Insert JSON Array Data to Database

From Dev

Insert data with associative array using PDO

From Dev

Insert data to sqlite from json array - UWP

From Dev

Scraping a Nested Data and Insert them into a Single Array

From Dev

Insert data into a dynamic array in Vue / Vuex

From Dev

Spring Data Mongo Template - Counting an array

From Dev

AngularJS insert data in nested array and sorting with a specific nested array object

From Dev

Insert array_intersect_key array data into a database table

From Dev

How to change array for insert array data to database in PHP?

Related Related

HotTag

Archive