条件表达式中的C++sizeof(typename T)
C++ sizeof( typename T) in a conditional expression
我在这里问了一个问题,C++模板参数和表达式,得到了答案,但现在我想转到这样的问题上:
#include <complex>
#include <type_traits>
using namespace std;
template<class T, class U>
complex< conditional<(sizeof( T ) > sizeof( U )), T, U>::type > my_method(T x, U y) { }
这给了我错误:
main.cpp:10:65: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::complex’
main.cpp:10:65: error: expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’
我尝试在T和U之前添加关键字typename和class,但这也改变了错误:
main.cpp:37:1: error: wrong number of template arguments (1, should be 3)
/usr/include/c++/4.7/type_traits:77:12: error: provided for ‘template<bool <anonymous>, class, class> struct std::conditional’
main.cpp:10:10: error: template argument 1 is invalid
main.cpp:10:1: error: expected unqualified-id at end of input
我在linux上使用带有-std=c++11的g++作为编译器选项。有没有办法做我想做的事?谢谢
正如编译器所说:
main.cpp:10:65: error: expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’
std::conditional<...
之前缺少类型名。
相关文章:
- 为什么返回类型中需要typename?C++
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 同一对象的"sizeof"的不同答案
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 使用 'typename' 关键字将非类型视为依赖上下文中的类型
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 在这种情况下,"typename..."意味着什么?
- 为什么不需要在 C++20 中的依赖类型之前指定"typename"?
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- "typename"不应该只在模板函数或模板类中使用吗?
- 为什么 MSVC _count_of实现将 0 添加到 sizeof 的结果中?
- alignas() 对 sizeof() 的影响 - 强制性的?
- 在C++中,运算符 sizeof 返回什么数据类型?
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 条件表达式中的C++sizeof(typename T)