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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:26:44+00:00 2026-06-09T22:26:44+00:00

C++ (more specifically, MinGW’s implementation of g++) is getting confused. I have a mathematical

  • 0

C++ (more specifically, MinGW’s implementation of g++) is getting confused. I have a mathematical Vector class that contains an arbitrary number of an arbitrary type of element. The element type and number of elements are specified at compile time.

The Vector class is getting confused between one of it’s constructors and what I have dubbed the “resize” operator. The resize operator allows the programmer to cast a vector of one size into a vector of another arbitrary size. If the cast vector has more elements than the base vector, it pads with 1’s. Here’s the implementation:

/*
 * resize operator:
 * T is the type of element the base vector holds
 * N is the number of elements the base vector holds
 * rN is the size of the new vector
 */
template<typename T, unsigned int N, unsigned int rN>
operator Vector<T, rN>() const 
{
    Vector<T, rN> resize;

    for (unsigned int i = 0; i < rN; i++)
    {
        resize[i] = i < N ? this->elements[i] : 1;
    }

    return resize;
}

The vector class also has a type-safe variadic constructor that can take any number of any combinations of elements (which must be of type T) and any number of Vectors (which can contain any number of elements and must be of type T) so long as the number of bare elements added to the number of elements in the supplied vectors is equal to the number of elements that the constructing vector contains.

Thus this would be valid:

vec3 foo(vec2(1, 2), 3);

but not this.

vec3 bar(vec4(1, 2, 3, 4), 5);

I ensure that the proper number of elements has been supplied at compile time by recursing through them all with a counter, then I use a static assertion to make sure the counter ends up at number of elements the vector can contain. This normally works out well, except for the following code:

vec4 bar(1, 2, 3, 4);
(vec3) bar; //PROBLEM HERE

What’s happening is that the C++ thinks that (vec3) bar is asking for the variadic constructor when, in fact, it should be calling the resize operator. I’ve tried making them explicit, but it hasn’t worked. How do I ensure that C++ uses the resize operator when I have the above code and not the variadic constructor?

In short, how do I tell C++ to use this:

//resize operator
template<typename T, unsigned int N, unsigned int rN>
Vector<T, N>::operator Vector<T, rN>();

instead of this:

//constructor
template<typename T, unsigned int N, typename ... Args>
Vector<T, N>::Vector(Args ... arguments);

when I have this code:

(vec3) someVec4;

In case it wasn’t clear, vec3 and vec4 are defined as such:

typedef Vector<float, 3> vec3;
typedef Vector<float, 4> vec4;

EDIT:

News, everyone! Even when I use static_cast(someVec4) it still calls the vec3 constructor with a vec4 argument. I don’t know why.

ANOTHER EDIT:

Making the constructor explicit allows implicit casts to work, but not explicit ones. Which is to say that this code works:

vec3 foo = someVec4;

But this code still gives me a static assertion failure:

vec3 foo = static_cast<vec3>(someVec4);

Which makes basically no sense because I declared the variadic constructor explicit, and thus it shouldn’t be called there.

Also, upon request, here’s an SSCCE

The TL;DR version of this is that my code is calling an explicit constructor when I try to explicitly call the typecasting operator, but not when I try to implicitly call it.

  • 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-09T22:26:45+00:00Added an answer on June 9, 2026 at 10:26 pm

    I think the simplest was to get your code working is to replace the conversion operator by a conversion constructor. Since that constructor is more specialized than the variadic constructor, it should always take precedence.

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

Sidebar

Related Questions

More specifically, does NDK have a complete STL implementation. We're looking at this for
I have an information system, more specifically an information system that is a ticketing
More specifically, let's say I have a class template with parameters A and B
More specifically, let's say I have a class template with parameters A and B
More specifically, a class library assembly. My initial thoughts: Have some designated administrators do
More specifically let's say I have a number of .py files, with main.py importing
More specifically, I have a right sidebar that spans the right side of the
More specifically what are the characteristics (if any) that delegates have that functions as
More specifically, if I have: public class TempClass : TempInterface { int TempInterface.TempProperty {
I am making a GUI in OpenGL (more specifically lwjgl). I have tried hard

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.