I was trying to static_assert that some meta transformer algorithm worked, and it incredibly did not compare to same, even though the typeid().name() returned the exact same string.
A repetition of the type expression in the typedef could fix the is_same, but I can't understand how repeating the initializer expression in the typedef changes the type, over taking the decltype of an auto variable initialized with that same expression.
A more concrete explanation of what I was doing: …
在使用 clang 编译器的 C++17 中,无论我这样做,我都会遇到相同的构建错误:
EXPECT_TRUE(std::is_same_v<decltype(var1), decltype(var2)>);
Run Code Online (Sandbox Code Playgroud)
或这个:
EXPECT_TRUE(typename std::is_same_v<decltype(var1), decltype(var2)>);,
Run Code Online (Sandbox Code Playgroud)
或这个:
EXPECT_TRUE(typename std::is_same_v<typename decltype(var1), typename decltype(var2)>);
Run Code Online (Sandbox Code Playgroud)
构建命令:
bazel test //my_target_dir:my_target
Run Code Online (Sandbox Code Playgroud)
构建错误:
Run Code Online (Sandbox Code Playgroud)error: too many arguments provided to function-like macro invocation decltype(var2)>); ^ gtest/gtest.h:1980:9: note: macro 'EXPECT_TRUE' defined here #define EXPECT_TRUE(condition) \ ^ myfile.cpp:125:5: error: use of undeclared identifier 'EXPECT_TRUE' EXPECT_TRUE(std::is_same_v< ^
请注意,Googletest 的定义EXPECT_TRUE()在这里:https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest.h#L1980。
我正在做的事情有什么问题,我怎样才能编译它?
如何检查两个类模板实例化是否属于同一个类模板。这是我的代码
#include <iostream>
#include <type_traits>
template<typename T1, typename T2>
class A {
float val;
public:
};
int main() {
A<double, double> a_float_type;
A<int, int> a_int_type;
// how to check whether both a_double_type and a_int_type were instantiated from the same template class A<,>
std::cout << std::is_same<decltype(a_float_type), decltype(a_int_type)>::value << std::endl; // returns false
}
Run Code Online (Sandbox Code Playgroud)
我的编译器只支持C++11
考虑我有结构 RGB 和 ARGB。
template<typename T>
struct RGB {
T r,g,b;
};
template<typename T>
struct ARGB {
T a,r,g,b;
}
Run Code Online (Sandbox Code Playgroud)
现在我通过以下来定义它们。
using RGB_888 = RGB<unsigned char>;
using RGB_Float = RGB<float>;
using ARGB_8888 = ARGB<unsigned char>;
using ARGB_Float = ARGB<float>;
Run Code Online (Sandbox Code Playgroud)
在某些时候,我想从一个 rgb 转换为另一个,从 rgb 转换为 argb。所以我做以下事情。
template<typename Source, typename Dest>
void convert(const Source& source, Dest& dest)
{
}
Run Code Online (Sandbox Code Playgroud)
它会像这样工作。
RGB_888 rgb888{123,22,311};
RGB_Float rgbFloat;
convert<RGB_888,RGB_Float>(rgb888,rgbFloat);
RGB_888 rgb888(123,22,132};
ARGB_Float argbFloat;
convert<RGB_888,ARGB_Float>(rgb888,argbFloat);
Run Code Online (Sandbox Code Playgroud)
问题是我无法检测typename Source和typename Dest是否来自相同的颜色模型,我需要正确转换。换句话说,如果一些typename …