Given a Function Parameter of Type [Int]; Can It Be Constrained to Not Be Empty

Generic constraints with C#

where TType : ParameterValue<TType>

This is a recursive generic constraint, which will simplify to TType : ParameterValue<XYZParameterValue> where XYZParameterValue : ParameterValue<TType> which is not what you want, because in your case the actual type (e.g. string) does not inherit its corresponding ParameterValue (ParameterValue<string>).

Your generic constraints would work when using a generic interface/base class which is implemented/inherited by the same type which it is generic over, like the IComparable<T> interface which is implemented by the type T (i.e. System.String : IComparable<System.String>).

Instead, I'd do the following:

public class Parameter<T> : Parameter
{
public Parameter(string name, ParameterValue<T> value) : base(name)
{
Value = value;
}

public ParameterValue<T> Value { get; set; }
}

You'd have to change ParameterManager methods to a similar form too:

public void AddParameter<T>(string name, ParameterValue<T> value)
{
_parameters[name] = new Parameter<TType>(name, value);
}

public ParameterValue<T> FindParameterValue<T>(string name)
{
var parameter = _parameters[name];
var parameterTyped = parameter as Parameter<TType>;
return parameterTyped?.Value;
}

Side Note: Naming the type constraint TType doesn't make any sense by general conventions since the T prefix in a type parameter means "type" and so T would be enough.

How do I constrain a template parameter to comply to a Key in std::map?

It depends on what you mean by "complies." If you want to verify that K has a < operator then you might try the Boost Concept Checking Library.

#include "boost/concept_check.hpp"

template< typename K >
class Foo
{
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< K >));

// lots of other code here...

private:
std::map< K, size_t > m_map;
};

However if you want to verify that < defines a StrictWeakOrdering on K, that would require testing run-time behaviour under all possible inputs.

Cannot use golang generics variable in function call

Why am I receiving that error

Because fn is a BoolFunc, which is a func(string) bool, thus requires a string as parameter, but input is of type T. Furthermore, by your definition, T satisfies the Inputer constraint, and thus can also assume type int, float64 or any non-string type which has string as its underlying type (~string) none of which implicitly cast to string.

how do I fix it?

You need to change the definition of BoolCase to itself have a generic type parameter for its one parameter. You could constraint it to Inputer, but you could also use any (interface{}).

type BoolCase[T any] func(T) bool

Then make sure to serve this generic type parameter in the signature of function EvalCases:

func EvalCases[T Inputer, U Wanter](cases TestConditions[T, U], fn BoolCase[T])

https://go.dev/play/p/RdjQXJ0WpDh

Nullable type as a generic parameter possible?

Change the return type to Nullable<T>, and call the method with the non nullable parameter

static void Main(string[] args)
{
int? i = GetValueOrNull<int>(null, string.Empty);
}

public static Nullable<T> GetValueOrNull<T>(DbDataRecord reader, string columnName) where T : struct
{
object columnValue = reader[columnName];

if (!(columnValue is DBNull))
return (T)columnValue;

return null;
}

Constrain non-type/value template parameter using requires in an ad-hoc fashion

If you wanted to constrain a template parameter, a single requires is enough:

template <GLenum shader_type>
requires(shader_type == GL_VERTEX_SHADER || shader_type == GL_FRAGMENT_SHADER)
void foo() {}

A function parameter can be constrained like this, at the cost of requiring it to be a compile-time constant:

struct ShaderType
{
GLenum value;
consteval ShaderType(GLenum value) : value(value)
{
if (shader_type != GL_VERTEX_SHADER && shader_type != GL_FRAGMENT_SHADER)
throw "Invalid value!";
// Normally it's not a good idea to throw pointers,
// but here it just used to stop the compilation.
}
};

void foo(ShaderType type) {}

Constrain total number of array elements less than predetermined value

Calling a user defined function in a constraint creates problems because the values for the input argument (in this case abs_vals) get picked before calling the function, and the output of the function gets treated as a nonrandom state value. That means if the values chosen for abs_vals do not meet the constraint no_more_than_p_small_values, the call to randomize will just fail and not try to find another set of values to meet the constraint. Its just a matter of luck that it might not fail the first few times you call it.

What you should do is try the built-in iterative methods like array.sum() which do not suffer from this ordering problem.

class InputData;
rand bit [3 : 0] abs_vals [64];
constraint no_more_than_p_small_values {
abs_vals.sum() with (int'(item<4)) <= 6;
}
function void display();
foreach (abs_vals[i]) $write("%1h", abs_vals[i]);
$write("\n");
endfunction
endclass

Are constraints in overload resolution affected by difference it type qualifiers?

Without considering constraints, the call is ambiguous because the first overload is deduced to a function parameter type const int& and the second to int. Neither will be considered better than the other when called with a prvalue of type int and neither const auto& or auto are more specialized in usual partial ordering of templates either.

According to [temp.func.order]/6.2.2 constraints on function templates are not taken into account if the types of the function parameters after substitution from template argument deduction do not correspond.

Here the first overload deduced the function parameter to const int& and the second to int. These are not equal. And so partial ordering of the templates will not consider either more specialized than the other based on any constraints either.



Related Topics



Leave a reply



Submit