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 7895549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:40:14+00:00 2026-06-03T07:40:14+00:00

I have a problem I came up with recently. I actually think it can

  • 0

I have a problem I came up with recently. I actually think it can not be solved as I would like it to be, but it would be quite handy if it could. Anyway, here’s the problem:

I will give you an example I’ve seen a few days ago on this forum since it will be easier to explain with it. Let’s say I’m trying to create a Tensor struct, this way:

template <int N>
struct Tensor
{
    Tensor<N - 1> x;
    Tensor<N - 1> y;
    Tensor<N - 1> z;
};

To avoid infinite recursion, I would have to write the template specialization for N = 1.

template<>
struct Tensor<1>
{
    double x;
    double y;
    double z;
};

Actually, when N = 1, this Tensor is actually a Vector (physical one). Let’s say I already have a Vector struct defined this way:

struct Vector
{
    double x;
    double y;
    double z;
};

This struct is exactly like Tensor<1>. Since the Vector struct already existed and, let’s say I did not implemented it myself, I would like to be able to make the Tensor<1> struct an alias of the Vector struct. Just like a typedef. So, I would like to do it this way:

// C++03
typedef Vector Tensor<1>;

// or C++11
using Tensor<1> = Vector;

This way, Tensor<1> and Vector would be the exact same struct, so it would I could use one instead of another in the program wherever I want and I would not have to write the same struct twice.

However, it’s actually impossible to define a template specialization that way. If it was, I would not be asking the question there.

Note: I know the previous example is not a good one since we can still do this:

using Vector = Tensor<1>;

But it is quite bothersome if I want to do it with specializations of two different structs. For example when writing a geometry library that could compute geometry in N-dimensional spaces:

using Circle<2> = Hypersphere<2>;

So, to sum up: is there a way to create template specializations by defining it as the alias of another one?

  • 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-06-03T07:40:15+00:00Added an answer on June 3, 2026 at 7:40 am

    Given legacy classes Scalar, Vector and Matrix, you could use inheritance:

    template<>
    class Tensor<0>: public Scalar {};
    
    template<>
    class Tensor<1>: public Vector {};
    
    template<>
    class Tensor<2>: public Matrix {};
    

    Note that this is not abuse of inheritance because you are modelling an is-a relationship.

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

Sidebar

Related Questions

I recently came across a problem for image file storage in network. I have
I recently came across this problem and found a solution but I'm wondering if
Possible Duplicate: Learning efficient algorithms I recently came across an problem that was solved
I came across an interesting problem recently. In an ASP.NET Master Page, I have
I recently came across a big problem, as I have a system that's paying
I'm new to Vim (almost new) and have some questions. The problem came from
While doing some small regex task I came upon this problem. I have a
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I recently started programming my first Cocoa app. I have ran into a problem
I recently made some changes to my development environment and came across a problem.

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.