Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 136423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:56:46+00:00 2026-05-11T06:56:46+00:00

I have a class template <unsigned int N> class StaticVector { // stuff };

  • 0

I have a class

template <unsigned int N> class StaticVector { // stuff }; 

How can I declare and define in this class a static factory method returning a StaticVector<3> object, sth like

StaticVector<3> create3dVec(double x1, double x2, double x2); 

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:56:46+00:00Added an answer on May 11, 2026 at 6:56 am

    ‘How can I declare and define in this class’

    In what class? You’ve defined a class template, not a class. You can’t call a static function of a class template itself, you have to call a particular version of the static function that’s part of a real class.

    So, do you want the template (and hence all instantiations of it) to have a function returning a StaticVector<3>, or do you want one particular instantiation of that template to have a function returning a StaticVector<3>?

    If the former:

       template <unsigned int N>   struct SV {     int contents[N];     static SV<3> get3dVec(int x, int y, int z) {       SV<3> v;       v.contents[0] = x;       v.contents[1] = y;       v.contents[2] = z;       return v;     }   };    int main() {     SV<3> v = SV<1>::get3dVec(1,2,3);   } 

    works for me.

    If the latter (you only want get3dVec to be a member of SV<3>, not of all SV<whatever>), then you want template specialisation:

       template <unsigned int N>   struct SV {     int contents[N];   };    template<>   struct SV<3> {     int contents[3]; // must be re-declared in the specialization     static SV<3> get3dVec(int x, int y, int z) {       SV<3> v;       v.contents[0] = x;       v.contents[1] = y;       v.contents[2] = z;       return v;     }   };    int main() {     SV<3> v = SV<1>::get3dVec(1,2,3); // compile error     SV<3> v = SV<3>::get3dVec(1,2,3); // OK   } 

    If for no other reason than to make the calling code look nicer by omitting the basically irrelevant template parameter, I agree with Iraimbilanja that normally a free function (in a namespace if you’re writing for re-use) would make more sense for this example.

    C++ templates mean that you don’t need static functions as much in C++ as you do in Java: if you want a ‘foo’ function that does one thing for class Bar and another thing for class Baz, you can declare it as a function template with a template parameter that can be Bar or Baz (and which may or may not be inferred from function parameters), rather than making it a static function on each class. But if you do want it to be a static function, then you have to call it using a specific class, not just a template name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class like below. template<int S> class A { private: char
The problem: I have a class which contains a template method execute which calls
I have a class like this: template <class T> class bag { private: typedef
I have this class for double linked lists: template <typename T> class Akeraios {
I have a template class that I serialize (call it C), for which I
I have a template class that is only valid for couple of template parameters:
I have a template class where I want to use objects of that class
I have a template class for thread-safe vector: template <class T> class SharedVector {
Let's say we have a class, MyParent: class MyParent { public: template<namespace T> MyParent()
My code is built to multiple .dll files, and I have a template class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.