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

  • SEARCH
  • Home
  • 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 1092745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:43:44+00:00 2026-05-16T23:43:44+00:00

Say you have a vector class that has a template length and type –

  • 0

Say you have a vector class that has a template length and type – i.e. vec<2,float>. These can also be nested – vec<2,vec<2,vec<2,float> > >, or vec<2,vec<2,float> >. You can calculate how deeply nested one of these vectors is like this:

template<typename T>
inline int depth(const T& t) { return 0; }
template<int N, typename T>
inline int depth(const vec<N,T>& v) { return 1+depth(v[0]); }

The trouble is you won’t know how deep it is until run-time, but you may need to know the depth at comile-time in order to do something like this:

// Do this one when depth(v1) > depth(v2)
template<int N, typename T, int M, typename U>
inline vec<N,T> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return v1 + coerce(v2,v1);
}
// Do this one when depth(v1) < depth(v2)
template<int N, typename T, int M, typename U>
inline vec<M,U> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return coerce(v1,v2) + v2;
}

You can’t just throw in an “if” statement because (a) which is deeper affects the return type and (b) coerce() generates a build error if you try to coerce a nested vector to a less-nested one.

Is it possible to do something like this or am I pushed up against the limits of C++ templates?

  • 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. Editorial Team
    Editorial Team
    2026-05-16T23:43:45+00:00Added an answer on May 16, 2026 at 11:43 pm

    It’s entirely possible. Try for example

    template<int N, typename T, int M, typename U>
    inline typename enable_if<is_deeper<T, U>::value, vec<N,T> >::type 
    operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
        return v1 + coerce(v2,v1);
    }
    
    template<int N, typename T, int M, typename U>
    inline typename enable_if<is_deeper<U, T>::value, vec<M,U> >::type 
    operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
        return coerce(v1,v2) + v2;
    }
    

    Where is_deeper is something like

    /* BTW what do you want to do if none is deeper? */
    template<typename T, typename U>
    struct is_deeper { static bool const value = false; };
    
    template<typename T, int N, typename U>
    struct is_deeper<vec<N, U>, T> { 
      static bool const value = true;
    };
    
    template<typename T, int N, typename U>
    struct is_deeper<T, vec<N, U> > { 
      static bool const value = false;
    };
    
    template<typename T, int N, int M, typename U>
    struct is_deeper<vec<M, T>, vec<N, U> > : is_deeper<T, U> 
    { };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a simple class with some storage data structure (list, vector, queue,
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Say I have: class Vector3 { float x, y, z; ... bunch of cuntions
Say I have a vector which has thousands of elements. What is the R
Lets say if I have a vector V, which has 10 elements. If I
I have a randomly generated vector, say A of length M . Say: A
Let's say we have a class that looks like this: class A { public:
I have 2 classes, say A & B. Class B has a destructor of
It may be a bit confusing, but... Let's say I have a vector type
Let's say I have a: std::vector<std::list<Node> > And let's say Node is: class Node

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.