更新:我修改了这里的代码,这样你就可以直接插入并尝试编译。
当在可变参数函数中使用时,Clang 3.4.1 (LLVM 3.4) 提示我的类不是 POD 类型(据我所知肯定是)。 G++ 编译这个没有问题。
即使按照 C++03 的标准,这个类也是 POD:
- 它只包含 PODS 作为成员
- 没有用户定义的析构函数
- 没有用户定义的复制赋值运算符
- 没有指向成员类型的非静态成员
而且 C++11 对“POD”的规定放宽了,所以这是怎么回事?
关于代码:这是 vector 的模板类(如 vector 图形),可变参数函数仅用于用一些任意数据填充数组。
我的类头文件:
template<typename TYPE>
class Vec3t;
typedef Vec3t<float> Vec3f;
template<typename TYPE>
class Vec3t {
public:
Vec3t<TYPE>( void ){}
Vec3t<TYPE>( const TYPE nx, const TYPE ny, const TYPE nz ){}
Vec3t<TYPE> operator-(void) const{}
Vec3t<TYPE> operator*( const Vec3t<TYPE> &other ) const{}
Vec3t<TYPE> operator*( const TYPE val ) const{}
Vec3t<TYPE>& operator*=( const Vec3t<TYPE> &other ){}
Vec3t<TYPE>& operator*=( const TYPE val ){}
Vec3t<TYPE> operator+( const Vec3t<TYPE> &other ) const{}
Vec3t<TYPE> operator+( const TYPE val ) const{}
Vec3t<TYPE>& operator+=( const Vec3t<TYPE> &other ){}
Vec3t<TYPE>& operator+=( const TYPE val ){}
Vec3t<TYPE> operator-( const Vec3t<TYPE> &other ) const{}
Vec3t<TYPE> operator-( const TYPE val ) const{}
Vec3t<TYPE>& operator-=( const Vec3t<TYPE> &other ){}
Vec3t<TYPE>& operator-=( const TYPE val ){}
Vec3t<TYPE> operator/( const Vec3t<TYPE> &other ) const{}
Vec3t<TYPE> operator/( const TYPE val ) const{}
Vec3t<TYPE>& operator/=( const TYPE val ){}
Vec3t<TYPE>& operator/=( const Vec3t<TYPE> &other ){}
bool operator==( const Vec3t<TYPE> &b ) const {}
bool operator==( const TYPE b ) const {}
bool operator!=( const Vec3t<TYPE> &b ) const {}
bool operator!=( const TYPE b ) const {}
Vec3t<TYPE>& Set( const TYPE a, const TYPE b, const TYPE c ){}
Vec3t<TYPE>& Set( const TYPE a, const TYPE b ){}
void Cross( const Vec3t<TYPE> &other, Vec3t<TYPE> &out ) const{}
TYPE Dot( const Vec3t<TYPE> &other ) const{}
Vec3t<TYPE> PerpCCW_ZAxis( void ){}
Vec3t<TYPE> PerpCW_ZAxis( void ){}
float Len( void ) const{}
void Zero( void ){}
static void Cross( const Vec3t<TYPE> &other, const Vec3t<TYPE> &u, Vec3t<TYPE> &out ){}
Vec3f Normalize( void ){}
Vec3f Rotation( TYPE angle ){}
float DegreesBetween( const Vec3f &other ){}
float RadiansBetween( const Vec3f &other ){}
Vec3f Rotate( float angle, const bool inRadians = false ){}
static float Len( const Vec3f &other ){}
public:
TYPE x,y,z;
};
template< typename TYPE >
void FillArray( const std::size_t count, TYPE* var, ... ) {
va_list vargs;
va_start( vargs, var );
for ( std::size_t i=0; i<count; i++ ) {
var[i] = va_arg( vargs, TYPE ); // ** THIS IS THE LINE CLANG ERRS ON ** //
}
va_end( vargs );
}
我的可变函数头文件:
#include <cstdarg>
template< typename TYPE >
void FillArray( const std::size_t count, TYPE* var, ... ) {
va_list vargs;
va_start( vargs, var );
for ( std::size_t i=0; i<count; i++ ) {
var[i] = va_arg( vargs, TYPE ); // ** THIS IS THE LINE CLANG ERRS ON ** //
}
va_end( vargs );
}
Clang 的错误: 错误:“va_arg”的第二个参数是非 POD 类型“Vec3t”[-Wnon-pod-varargs]
使用这些导致 Clang 错误的示例程序: #include "fillarray.h"
int main ( void ) {
Vec3t<float> v[2];
FillArray< Vec3t<float> >( 2, v, 0.4f, 0.5f);
}
编译: clang++ x.cpp -I/usr/include/i386-linux-gnu/c++/4.8 -std=gnu++11
最佳答案
§12.1/7:
A default constructor is trivial if it is not user-provided and...
Otherwise, the default constructor is non-trivial.
根据定义,您声明的默认构造函数是非平凡的。
§9/3:
A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.
由于默认构造函数是非平凡的,因此该类不是平凡的。
§9/10:
A POD struct is a non-union class that is both a trivial class and a standard-layout class...
由于该类不是微不足道的,因此它不符合 POD 的条件。为了使其成为 POD,您必须摆脱默认构造函数。
关于c++ - Clang 提示 POD 类实际上是非 POD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18806954/