1
我正在玩使用gcc 4.9.2的ORM設計。我有一個類的2名成員:(TableBase是一類我使用了一些靜態函數和子類的聲明爲類範圍拉它沒有任何成員或方法,只有靜態功能和子類的聲明。)constexpr類模板文字類型可以在非文字類型上參數化嗎?
class Staff : public Db::TableBase<Staff> {
public:
long int staffId_;
std::string nickname_;
}
我對每個成員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類型information.The定義:
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");
}
正常工作在這裏:http://coliru.stacked-crooked.com/a/b540beaa39e323b0 –
這個問題似乎是在靜態constexpr。我已將我的原始代碼編輯爲完整的自包含示例。 – user2352497
在這裏工作很好,在叮噹:) - > http://coliru.stacked-crooked.com/a/b9a3102e4849b0d0 –