我正在使用gcc 4.9.2进行ORM设计。我有一个2人的班级:
class Staff : public Db::TableBase<Staff> {
public:
long int staffId_;
std::string nickname_;
}
(TableBase是我用来将几个静态函数和子类声明引入类范围的类。它没有成员或方法,只有静态函数和子类声明。)
我为每个成员都有一个ColumnObject:
static constexpr auto colTest1=Db::detail::ColumnObject<Staff,long int>
(&Staff::staffId_,"staff_id",Db::BigSerial | Db::Default,Db::Transport::TEXT);
static constexpr auto colTest2=Db::detail::ColumnObject<Staff, std::basic_string<char> >
(&Staff::nickname_,"nick_name",Db::NotNull,Db::Transport::TEXT);
第一个语句编译,第二个语句不编译:
~/git/WebSchedule2/src/common/ORM/Staff.hpp:110:152: error: ‘const Db::detail::ColumnObject<Staff, std::basic_string<char> >{&Staff::nickname_, ((const char*)"nick_name"), 1, (Db::Transport)0, -1}’ is not a constant expression
static constexpr auto colTest2=Db::detail::ColumnObject<Staff, std::basic_string<char> >(&Staff::nickname_,"nick_name",Db::NotNull,Db::Transport::TEXT);
你可能会说第二个语句没有编译的原因是因为std :: string的类型依赖使它成为非文字。但是,我实际上并没有在ColumnObject的定义中使用字符串,我只使用了类型信息.ColumnObject的定义:
template<typename Derived,typename MemberType>
class ColumnObject {
public:
MemberType Derived::*memberPtr_;
char const *columnName_;
int const traits_;
Db::Transport transport_;
int columnNumber_;
constexpr ColumnObject()
: memberPtr_(nullptr), columnName_("UNITIALIZED COLUMN"), traits_(-1),
transport_(Db::Transport::TEXT), columnNumber_(-1) { }
constexpr ColumnObject(MemberType Derived::*memberPtr, char const *columnName,
int const traits, Db::Transport transport)
: memberPtr_(memberPtr), columnName_(columnName), traits_(traits),
transport_(transport), columnNumber_(-1) { }
}
我有什么想法可以解决这个问题吗?
(编辑:我在下面提供了一个完整的示例。问题似乎是使用静态constexprs。)
// g++ -Wall -Wextra -pedantic -std=c++14 -c foo.cpp
#include <string>
template<typename Derived,typename MemberType>
class ColumnObject;
template<typename Derived,typename MemberType>
class ColumnObject {
public:
MemberType Derived::*memberPtr_;
char const *columnName_;
constexpr ColumnObject()
: memberPtr_(nullptr), columnName_("UNITIALIZED COLUMN") { }
constexpr ColumnObject(MemberType Derived::*memberPtr, const char *columnName)
: memberPtr_(memberPtr), columnName_(columnName) { }
};
class Staff {
public:
long int staffId_;
std::string nickname_;
Staff(){
staffId_=0;
nickname_="";
}
Staff(long int snid, std::string nname)
: staffId_(snid), nickname_(nname)
{
};
static constexpr auto colTest1=ColumnObject<Staff,long int>(&Staff::staffId_,"staff_id");
static constexpr auto colTest2=ColumnObject<Staff, std::string >(&Staff::nickname_,"nick_name");
};
int main(int argc, char *argv[]){
constexpr auto colTest3=ColumnObject<Staff,long int>(&Staff::staffId_,"staff_id");
constexpr auto colTest4=ColumnObject<Staff, std::string >(&Staff::nickname_,"nick_name");
}
答案 0 :(得分:0)
在这里工作正常,在clang :) - &gt; coliru.stacked-crooked.com/a/b9a3102e4849b0d0 - pepper_chico
现在可以在RedHat gcc 5.3.1-2中使用。 - user2352497