How to set std::array size via parameter pack arguments?












1















I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array member variable depending on the values in the parameter pack? As far as I understand the values in the parameter pack should be known at compile time.



template<size_t N>
class Matrix {
public:
template<typename... Exts>
Matrix(Exts... exts) : dimSizes{exts...} { }

private:
std::array<size_t, N> dimSizes;
std::array<float, N> data;
// e.g something like this: std::array<float, dimSizes[0]> data;
};

int main(void) {
Matrix<3> mat(2, 3, 2);
return 0;
}









share|improve this question

























  • Can't you use the initialisation list of the constructor?

    – Jesper Juhl
    Nov 25 '18 at 18:11











  • Sort of in c++17, no (mostly) before. Unless I warp your question.

    – Yakk - Adam Nevraumont
    Nov 25 '18 at 18:13








  • 3





    You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

    – Igor Tandetnik
    Nov 25 '18 at 18:14











  • @JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

    – SPA
    Nov 25 '18 at 18:15













  • @SPA what stops you from using std::vector<float>(dimSizes[0]) ?

    – Piotr Skotnicki
    Nov 25 '18 at 18:23


















1















I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array member variable depending on the values in the parameter pack? As far as I understand the values in the parameter pack should be known at compile time.



template<size_t N>
class Matrix {
public:
template<typename... Exts>
Matrix(Exts... exts) : dimSizes{exts...} { }

private:
std::array<size_t, N> dimSizes;
std::array<float, N> data;
// e.g something like this: std::array<float, dimSizes[0]> data;
};

int main(void) {
Matrix<3> mat(2, 3, 2);
return 0;
}









share|improve this question

























  • Can't you use the initialisation list of the constructor?

    – Jesper Juhl
    Nov 25 '18 at 18:11











  • Sort of in c++17, no (mostly) before. Unless I warp your question.

    – Yakk - Adam Nevraumont
    Nov 25 '18 at 18:13








  • 3





    You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

    – Igor Tandetnik
    Nov 25 '18 at 18:14











  • @JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

    – SPA
    Nov 25 '18 at 18:15













  • @SPA what stops you from using std::vector<float>(dimSizes[0]) ?

    – Piotr Skotnicki
    Nov 25 '18 at 18:23
















1












1








1








I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array member variable depending on the values in the parameter pack? As far as I understand the values in the parameter pack should be known at compile time.



template<size_t N>
class Matrix {
public:
template<typename... Exts>
Matrix(Exts... exts) : dimSizes{exts...} { }

private:
std::array<size_t, N> dimSizes;
std::array<float, N> data;
// e.g something like this: std::array<float, dimSizes[0]> data;
};

int main(void) {
Matrix<3> mat(2, 3, 2);
return 0;
}









share|improve this question
















I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array member variable depending on the values in the parameter pack? As far as I understand the values in the parameter pack should be known at compile time.



template<size_t N>
class Matrix {
public:
template<typename... Exts>
Matrix(Exts... exts) : dimSizes{exts...} { }

private:
std::array<size_t, N> dimSizes;
std::array<float, N> data;
// e.g something like this: std::array<float, dimSizes[0]> data;
};

int main(void) {
Matrix<3> mat(2, 3, 2);
return 0;
}






c++ c++11 variadic-templates variadic-functions stdarray






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 18:43









max66

37.2k74167




37.2k74167










asked Nov 25 '18 at 18:10









SPASPA

154




154













  • Can't you use the initialisation list of the constructor?

    – Jesper Juhl
    Nov 25 '18 at 18:11











  • Sort of in c++17, no (mostly) before. Unless I warp your question.

    – Yakk - Adam Nevraumont
    Nov 25 '18 at 18:13








  • 3





    You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

    – Igor Tandetnik
    Nov 25 '18 at 18:14











  • @JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

    – SPA
    Nov 25 '18 at 18:15













  • @SPA what stops you from using std::vector<float>(dimSizes[0]) ?

    – Piotr Skotnicki
    Nov 25 '18 at 18:23





















  • Can't you use the initialisation list of the constructor?

    – Jesper Juhl
    Nov 25 '18 at 18:11











  • Sort of in c++17, no (mostly) before. Unless I warp your question.

    – Yakk - Adam Nevraumont
    Nov 25 '18 at 18:13








  • 3





    You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

    – Igor Tandetnik
    Nov 25 '18 at 18:14











  • @JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

    – SPA
    Nov 25 '18 at 18:15













  • @SPA what stops you from using std::vector<float>(dimSizes[0]) ?

    – Piotr Skotnicki
    Nov 25 '18 at 18:23



















Can't you use the initialisation list of the constructor?

– Jesper Juhl
Nov 25 '18 at 18:11





Can't you use the initialisation list of the constructor?

– Jesper Juhl
Nov 25 '18 at 18:11













Sort of in c++17, no (mostly) before. Unless I warp your question.

– Yakk - Adam Nevraumont
Nov 25 '18 at 18:13







Sort of in c++17, no (mostly) before. Unless I warp your question.

– Yakk - Adam Nevraumont
Nov 25 '18 at 18:13






3




3





You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

– Igor Tandetnik
Nov 25 '18 at 18:14





You can't have sizeof(Matrx<3>) magically vary depending on how an instance was constructed.

– Igor Tandetnik
Nov 25 '18 at 18:14













@JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

– SPA
Nov 25 '18 at 18:15







@JesperJuhl I don't think so. I have to specify the size of the std::array via a template argument. So I have to specify the size as soon as I declare it.

– SPA
Nov 25 '18 at 18:15















@SPA what stops you from using std::vector<float>(dimSizes[0]) ?

– Piotr Skotnicki
Nov 25 '18 at 18:23







@SPA what stops you from using std::vector<float>(dimSizes[0]) ?

– Piotr Skotnicki
Nov 25 '18 at 18:23














1 Answer
1






active

oldest

votes


















1















Is it possible to set the size of the std::array member variable depending on the values in the parameter pack?



// e.g something like this: std::array<float, dimSizes[0]> data;




No, as far I know is impossible exactly as you want.



Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.



If you want a std::array with different size, you have to differentiate the types; so the dimension for the second std::array has to be a template parameter.



Obviously you can substitute the std::array with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector






share|improve this answer





















  • 2





    "Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

    – SPA
    Nov 25 '18 at 18:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470411%2fhow-to-set-stdarray-size-via-parameter-pack-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















Is it possible to set the size of the std::array member variable depending on the values in the parameter pack?



// e.g something like this: std::array<float, dimSizes[0]> data;




No, as far I know is impossible exactly as you want.



Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.



If you want a std::array with different size, you have to differentiate the types; so the dimension for the second std::array has to be a template parameter.



Obviously you can substitute the std::array with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector






share|improve this answer





















  • 2





    "Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

    – SPA
    Nov 25 '18 at 18:39
















1















Is it possible to set the size of the std::array member variable depending on the values in the parameter pack?



// e.g something like this: std::array<float, dimSizes[0]> data;




No, as far I know is impossible exactly as you want.



Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.



If you want a std::array with different size, you have to differentiate the types; so the dimension for the second std::array has to be a template parameter.



Obviously you can substitute the std::array with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector






share|improve this answer





















  • 2





    "Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

    – SPA
    Nov 25 '18 at 18:39














1












1








1








Is it possible to set the size of the std::array member variable depending on the values in the parameter pack?



// e.g something like this: std::array<float, dimSizes[0]> data;




No, as far I know is impossible exactly as you want.



Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.



If you want a std::array with different size, you have to differentiate the types; so the dimension for the second std::array has to be a template parameter.



Obviously you can substitute the std::array with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector






share|improve this answer
















Is it possible to set the size of the std::array member variable depending on the values in the parameter pack?



// e.g something like this: std::array<float, dimSizes[0]> data;




No, as far I know is impossible exactly as you want.



Because, this way, different instances of the same class would contain members with same name but different types. Strictly forbidden in a strongly typed language as C++.



If you want a std::array with different size, you have to differentiate the types; so the dimension for the second std::array has to be a template parameter.



Obviously you can substitute the std::array with a container that doesn't depend from the size; as suggested by Piotr Skotnicki a possible solution is std::vector







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 18:35

























answered Nov 25 '18 at 18:29









max66max66

37.2k74167




37.2k74167








  • 2





    "Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

    – SPA
    Nov 25 '18 at 18:39














  • 2





    "Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

    – SPA
    Nov 25 '18 at 18:39








2




2





"Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

– SPA
Nov 25 '18 at 18:39





"Because, this way, different instances of the same class would contain members with same name but different types." Good argument. I completly missed that.

– SPA
Nov 25 '18 at 18:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470411%2fhow-to-set-stdarray-size-via-parameter-pack-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'