0

For example, say we have this code

template<typename T>
class Base_Counter_A
{
// Only derived classes can instantiate this, to insure correct values are passed
protected:
    Base_Counter_A(T TValue):
        Val(TValue) 
    {
        //implementation
    }

    Base_Counter_A():
        Val(0)
    {
        //implementation
    }
private:
    int Val;
};

template<typename T, typename K>
class Counter : public Base_Counter_A<T>
{
public:
    Counter(T TValue, K KValue)
    requires(!std::is_same<T, int>::value):
        Base_Counter_A<T>() // Call Default constructor
    {
        // implementation
    }

    Counter(T TValue)
    requires(std::is_same<T, int>::value):
        Base_Counter_A<T>(TValue)   // Call overloaded Constructor
    {
        // Implementation
    }
};

Counter<int, char> Counter_Instance(2, 10); // Error C7500: no function satisfied its constraints
Counter<int, char>(2);                      // Success
Counter<std::string, char> Counter_Instance("Derived", 10); // Success

What I want to do is to add information to Error C7500, explaining that a second value cannot be passed when the first type is an int.

This is purely a simplified version of what my actual code looks like. Counter actually derives from two parent classes, similarly to how it derives from Base_Counter_A.

I've considered explicit specialization, but the number of permutations is extensive. I have solved the error by using requires, but would like to streamline this process and be as informative as possible when an incorrect value is passed.

Is it possible to provide the compiler additional information when error C7500 occurs. ie. "Error: C7500, too many parameters. Parameter 2 (char) was unexpected while parameter 1 is an integer"

Atijaf
  • 31
  • 5
  • We always had these options using SFINAE, using c++20 concepts just makes that easier. – πάντα ῥεῖ Sep 11 '22 at 20:58
  • I've given that some thought and that brings up another problem I would run into. My new problem would be that the "Counter" class derives from two parent classes, each of which may or may not take a value, based on the types of 'T' and 'K'. I will update my question to include that. – Atijaf Sep 11 '22 at 21:11

0 Answers0