How to pass constexpr as template parameter?


Peter Bell

I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal.

My code so far:

template <int T>
class MyClass { /*...*/ };


constexpr int PARAMS[] = {1,2,3 /*, ...*/};
for (constexpr auto& t: PARAMS) {
    MyClass<t> myClass;
    // ... do sth
}

But the compiler (gcc v4.9.2, C++11) doesn't accept it. I also tried using constinstead constexprit doesn't work.

Is this possible? I really don't want to use macros at all.

Pete Scottnick
#include <utility>
#include <cstddef>

constexpr int PARAMS[] = { 1, 2, 3, /*...*/ };

template <int N>
void bar()
{
    MyClass<N> myClass;
    // do sth
}

template <std::size_t... Is>
void foo(std::index_sequence<Is...>)
{
    using dummy = int[];    
    static_cast<void>(dummy{ 0, (bar<PARAMS[Is]>(), 0)... });

    // (bar<PARAMS[Is]>(), ...); since C++1z
}

int main()
{
    foo(std::make_index_sequence<sizeof(PARAMS)/sizeof(*PARAMS)>{});
    //                          <std::size(PARAMS)> since C++1z
    //                          <PARAMS.size()> for std::array<int,N> PARAMS{};
}

demo

Related


How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr as template parameter?

Peter Bell I have a templated class MyClassthat I want to run against various parameters to measure some values. I know the exact parameters before compiling, so I figured there must be a way to achieve my goal. My code so far: template <int T> class MyClass {

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass constexpr string array as parameter?

who I am I have a file that contains an array of strings representing some icons. static constexpr char icons1[2][40] = { "icon1_A", "icon1_B" }; static constexpr char icons2[3][30] = { "icon22_A", "icon2_B", "icons2_C" }; Then I have a class that I want

How to pass enum to template parameter

Zach I have an xml that is read from the type of object that needs to be created, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc..

How to pass template as parameter to function

Goodfellas 95 // Example program #include <iostream> #include <string> #include <array> using namespace std; int returnSize(template z <class T, size_t>) { /*if(arr.size() ==0) return 1; else return 2; */ return 1; } int main() {

How to pass enum to template parameter

Zach I have an xml that is read from the type of object I need to create, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc...

How to pass enum to template parameter

Zach I have an xml that is read from the type of object I need to create, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc...

How to pass enum to template parameter

Zach I have an xml that is read from the type of object I need to create, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc...

How to pass template as parameter to function

Goodfellas 95 // Example program #include <iostream> #include <string> #include <array> using namespace std; int returnSize(template z <class T, size_t>) { /*if(arr.size() ==0) return 1; else return 2; */ return 1; } int main() {

How to pass float as template parameter?

when I'm working on a metaprogramming project and we want to be able to pass floats as template parameters. I'm not sure if this question is a better fit for Stack Exchange, but I thought it was a conceptual choice and thus chose this site. Basically, I want t

How to pass float as template parameter?

when I'm working on a metaprogramming project and we want to be able to pass floats as template parameters. I'm not sure if this question is a better fit for Stack Exchange, but I thought it was a conceptual choice and thus chose this site. Basically, I want t

How to pass enum to template parameter

Zach I have an xml that is read from the type of object that needs to be created, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc..

How to pass enum to template parameter

Zach I have an xml that is read from the type of object that needs to be created, the problem is how can I pass the enum without using a switch/if statement. enum ObjectType {A,B,C}; void parseXML(const string& fileName) { //Open-read file etc..

How to pass float as template parameter?

when I'm working on a metaprogramming project and we want to be able to pass floats as template parameters. I'm not sure if this question is a better fit for Stack Exchange, but I thought it was a conceptual choice and thus chose this site. Basically, I want t

constexpr template parameter weirdness

vpozdyayev GCC (5.3) and Clang (3.8) claim that the first line testis bad, but the second line is good. MSVC (2015.2) says that both are invalid. template< typename N, typename T > void f( N n, T t ) { std::get< n >( t ); } void test() { std::get< std::int