std::is_same
std::is_same
is a type trait in C++ that allows you to compare two types for exact equality. It's part of the
#include <iostream>
#include <type_traits>
int main() {
bool result1 = std::is_same<int, int>::value;
bool result2 = std::is_same<int, double>::value;
std::cout << "Is int the same as int? " << (result1 ? "Yes" : "No") << std::endl;
std::cout << "Is int the same as double? " << (result2 ? "Yes" : "No") << std::endl;
return 0;
}
This example demonstrates the basic usage of std::is_same
:
- We compare int
with int
, which yields true
.
- We compare int
with double
, which yields false
.
- The ::value
member gives us a boolean result of the comparison.
#include <iostream>
#include <type_traits>
#include <string>
template<typename T>
void checkType() {
if (std::is_same<T, int>::value) {
std::cout << "T is an int" << std::endl;
} else if (std::is_same<T, double>::value) {
std::cout << "T is a double" << std::endl;
} else if (std::is_same<T, std::string>::value) {
std::cout << "T is a std::string" << std::endl;
} else {
std::cout << "T is some other type" << std::endl;
}
}
int main() {
checkType<int>();
checkType<double>();
checkType<std::string>();
checkType<float>();
return 0;
}
This example shows how std::is_same
can be used in template functions:
- We define a template function checkType
that uses std::is_same
to determine the type of T
.
- The function prints different messages based on whether T
is an int
, double
, std::string
, or some other type.
- In main()
, we call checkType
with different type arguments to demonstrate its behavior.
std::enable_if
and std::is_same
#include <iostream>
#include <type_traits>
// Function for int type
template<typename T>
typename std::enable_if<std::is_same<T, int>::value, void>::type
printTypeInfo(T value) {
std::cout << "This is an integer: " << value << std::endl;
}
// Function for double type
template<typename T>
typename std::enable_if<std::is_same<T, double>::value, void>::type
printTypeInfo(T value) {
std::cout << "This is a double: " << value << std::endl;
}
// Function for other types
template<typename T>
typename std::enable_if<!std::is_same<T, int>::value && !std::is_same<T, double>::value, void>::type
printTypeInfo(T value) {
std::cout << "This is neither an int nor a double" << std::endl;
}
int main() {
printTypeInfo(42);
printTypeInfo(3.14);
printTypeInfo(std::string("Hello"));
return 0;
}
This example demonstrates how std::is_same
can be used with std::enable_if
for SFINAE:
- We define three overloads of printTypeInfo
, each enabled for different types using std::enable_if
and std::is_same
.
- The first overload is enabled only for int
types.
- The second overload is enabled only for double
types.
- The third overload is enabled for all other types.
- In main()
, we call printTypeInfo
with different types to show how the correct overload is selected at compile-time.
#include <iostream>
#include <type_traits>
template<typename T, typename U>
constexpr bool are_same_types() {
return std::is_same<T, U>::value;
}
int main() {
constexpr bool result1 = are_same_types<int, int>();
constexpr bool result2 = are_same_types<int, double>();
static_assert(result1, "int and int should be the same type");
static_assert(!result2, "int and double should not be the same type");
std::cout << "All static assertions passed!" << std::endl;
return 0;
}
This example shows how std::is_same
can be used in constant expressions:
- We define a constexpr
function template are_same_types
that uses std::is_same
to compare types.
- In main()
, we use this function with constexpr
variables to perform compile-time type comparisons.
- We use static_assert
to verify our type comparisons at compile-time.
- If all assertions pass, the program will compile and print a success message.
std::is_same
is a powerful type trait in C++ that allows for compile-time type comparisons. It's particularly useful in template metaprogramming, SFINAE contexts, and for writing more generic and type-safe code. The examples provided demonstrate its basic usage, application in template functions, use with SFINAE and std::enable_if
, and its capability in constant expressions.
Key points to remember:
1. std::is_same
compares two types for exact equality.
2. It's often used with ::value
to get a boolean result.
3. It can be combined with other type traits and template metaprogramming techniques for more complex type manipulations.
4. std::is_same
is evaluated at compile-time, making it useful for static assertions and optimizations.
By mastering std::is_same
, C++ developers can write more flexible, type-safe, and efficient code, especially when working with templates and generic programming.