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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:12:24+00:00 2026-05-26T16:12:24+00:00

I have some C++ code that implements a basic Pixel class. Here’s just a

  • 0

I have some C++ code that implements a basic Pixel class. Here’s just a stub of the important bits I have:

// Pixel.h
template <class SizeType>
struct Pixel
{
public:
    typedef SizeType Size;
    Size R;
    Size G;
    Size B;
    static Size Min;
    static Size Max;

    TPixel(Size R = Min, Size G = Min, Size B = Min);
};

#include "Pixel.impl"


// Pixel.impl
template <class SizeType> SizeType Pixel<SizeType>::Min = std::numeric_limits<Size>::min();
template <class SizeType> SizeType Pixel<SizeType>::Max = std::numeric_limits<Size>::max();
template <> float Pixel<float>::Min = 0;
template <> float Pixel<float>::Max = 1;
template <> double Pixel<double>::Min = 0;
template <> double Pixel<double>::Max = 1;

And in C# I’m trying to duplicate this:

struct Pixel<Size> where Size : struct
{
    public Size R;
    public Size G;
    public Size B;
    public static const Size Min; // What should I do here?
    public static const Size Max;
}

Except I have no idea how I can include this Min / Max size into the type. I’d like to be able to have a uniform Pixel interface which allows you do things like clamping, adding, scaling, etc.

I’m running into a similar solution when I’m trying to deal with Vectors and Matrices of potentially arbitrary type. Can anyone advise me on how I can accomplish what the above C++ code is doing?

  • 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-26T16:12:24+00:00Added an answer on May 26, 2026 at 4:12 pm

    Unlike C++ template specialization, there is no such thing as specialization of a generic in .Net.

    You can’t initialize a T instance to anything but a T, unless you have a constraint, so you can’t use a static constructor to try to work around the problem.

    As far as I know, there is no constraint that allows you to specify only types that either are numeric types, or have conversions from numeric types.

    I suggest you just make two different types of classes:

    public struct PixelFloat
    {
        public float R { get; set; }
        public float G { get; set; }
        public float B { get; set; }
        public const float Min = 0.0f;
        public const float Max = 1.0f;
    }
    
    public struct PixelDouble
    {
        public double R { get; set; }
        public double G { get; set; }
        public double B { get; set; }
        public const double Min = 0.0f;
        public const double Max = 1.0f;
    }
    

    This is effectively the same thing anyhow, as this is what it would compile to under the covers. Other solutions won’t buy you much compared to this, as you’ll still have to type in the type: Pixel<double> Pixel<float>.

    Also, in cases like this, I would suggest you use names that show that you’re using types from your generic parameters. Size isn’t obviously a generic parameter. TSize is. And TSize doesn’t describe what the type does, it just describes how it varies. Instead you should name it something like TValue.

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

Sidebar

Related Questions

I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that uses the shared gateway pattern to implement an inversion
I've been trying to implement unit testing and currently have some code that does
I have some code that gives a user id to a utility that then
I have some code that raises PropertyChanged events and I would like to be
I have some code that generates image of a pie chart. It's a general
I have some code that effectively does this : File file = new File(C:\\Program
I have some code that I am putting in the code-behind of a master
I have some code that uses the Oracle function add_months to increment a Date
I have some code that prints out databse values into a repeater control on

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.