Is there a good way to evaluate an explicit constructor exists for multiple arguments? This is very similar to this question, except that std::is_convertible
won't work for this case because we have multiple arguments being passed to the constructor we're testing for.
For example:
#include <iostream>
#include <type_traits>
struct InitParams
{
int Parameter1;
int Parameter2;
};
class ExampleFloatConstructible
{
public:
explicit ExampleFloatConstructible(float InValue, const InitParams& InParams);
};
class ExampleIntConstructible
{
public:
explicit ExampleIntConstructible(int InValue, const InitParams& InParams);
};
template<typename ClassToTest, typename ArgType>
struct IsExplicitlyConstructibleWithSettings
{
static constexpr bool value = std::is_constructible<ClassToTest, ArgType, const InitParams&>::value;
};
int main()
{
// "Correct" values:
// will be true:
std::cout << "ExampleFloatConstructible Can Be Built from float? " <<
IsExplicitlyConstructibleWithSettings<ExampleFloatConstructible, float>::value << std::endl;
// will be true:
std::cout << "ExampleIntConstructible Can Be Built from int? " <<
IsExplicitlyConstructibleWithSettings<ExampleIntConstructible, int>::value << std::endl;
// "Incorrect" values:
// will also be true, because int is convertible from float
std::cout << "ExampleIntConstructible Can Be Built from float? " <<
IsExplicitlyConstructibleWithSettings<ExampleIntConstructible, float>::value << std::endl;
// will also be true, because float is convertible from int
std::cout << "ExampleFloatConstructible Can Be Built from int? " <<
IsExplicitlyConstructibleWithSettings<ExampleFloatConstructible, int>::value << std::endl;
}