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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:19:56+00:00 2026-06-06T09:19:56+00:00

In order to deal with vectors and fixed/dynamic allocation in some linear algebra problems,

  • 0

In order to deal with vectors and fixed/dynamic allocation in some linear algebra problems, I built the following classes (which I would prefer not to modify ):

// Traits : n is the size of the vector, if -1 : dynamic allocation
template<typename RealType, int n = -1>
struct ClassAVectorTraits
{
  typedef typename LinearAlgebraLibrary::FixedSizeVector<RealType, n> type;
};

template<typename RealType>
struct ClassAVectorTraits<T, -1>
{
  typedef typename LinearAlgebraLibrary::DynamicSizeVector<RealType> type;
};

template<typename RealType>
struct ClassAVectorTraits<T, 1>
{
  typedef typename RealType type;
};

// Implementation
template<typename RealType, int n = -1>
struct ClassA
{
  typedef typename ClassAVectorTraits<RealType, n>::type vector;

  void doSomething( const vector& vec )
    {
      ...
    }
};

Moreover I have an interface class (which I can modify) :

template<typename RealType, int n = -1>
struct UserClass
{
  typedef typename ClassAVectorTraits<RealType, n>::type vector;

  ClassA ca;

  void doSomething( const vector& vec )
    {
      ca.doSomething( vec );
    }

};

Now I want the user to be able to give STL vectors in input instead of LinearAlgebraLibrary vectors, so I did the following :

// A class to do automatic conversion
template<typename RealType, int n = -1>
struct Field : public ClassAVectorTraits<RealType,n>::type
{
  // Constructor NOT explicit
  Field( const std::vector<RealType>& vec )
    {
      // Copy vec into this (a LinearAlgebraLibrary vector)
    }
}

template<typename RealType>
struct Field<RealType, 1> { }; // Can't derive from integral type : RealType !

// And to classes to tag the solution 
template<typename RealType, int n>
struct USE_LINEAR_ALGEBRA_LIBRARY
{
  typedef typename ClassAVectorTraits<RealType,n>::type vector;
};

template<typename RealType, int n>
struct USE_STL
{
  typedef typename Field<RealType,n> vector;
};

And finally :

template<typename RealType, int n = -1, class VectorTag = USE_LINEAR_ALGEBRA_LIBRARY<RealType, n> >
struct UserClass
{
  typedef typename VectorTag::vector vector;

  ClassA ca;

  void doSomething( const vector& vec )
    {
      ca.doSomething( vec );
    }

};

And therefore, if we use the USE_STL tag, there is an automatic conversion :

UserClass<double, 2, USE_STL<double, 2> > userClass;
std::vector<double> vecSTL(2, 12.0);

userClass.doSomething( vecSTL ); // vecSTL is converted to LinearAlgebraLibrary::FixedSizeVector<double, 2>

My question : how can I deal with the n=1 case in which the vector type is an integral type. How can I define an implicit conversion between STL vector of double (size == 1) with a double ?

Any suggestions ? Thanks.

  • 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-06T09:19:57+00:00Added an answer on June 6, 2026 at 9:19 am

    You don’t have to follow the scheme of generic template when you are about to specialize. Hope this helps:

    template<typename RealType>
    struct Field<RealType, 1> { 
       typename ClassAVectorTraits<RealType,1>::type value;
       operator ClassAVectorTraits<RealType,1>::type () const { return value; }
       Field(const std::vector<RealType>& vec) 
       {
           if (vec.size() == 1) value = vec[0];
           else throw runtime_error("wrong size");
       }
    }; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used following code snippet for my activity in order to deal with orientation
Version Dependent Some of the answers to this question deal with older versions of
Order by descending is not working on LINQ to Entity In the following Query
I am doing a puzzle where I have to deal with numbers of order
I would like to know how you normally deal with this situation: I have
I'm looking for advice in order to deal with WCF Authentication for an application
I need some help with sending email when an order is placed. To illustrate
Here's the deal: I got two db models, let's say ShoppingCart and Order .
I'm used to using synchronous sockets. In order to deal with messages that have
Here's the deal : in order to build a correct series of parameters, i'm

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.