![](/img/trans.png)
[英]Assignment Operator with Template classes - no acceptable conversion, C++
[英]C++ typeid template value into union no acceptable conversion
我正在嘗試構建一個從 XML Blender 場景中讀取的屬性字典,並且遇到了模板問題並獲取了它們的 type_info。 包含的映射被設置為屬性的名稱,它的值存儲在 typeinfo 和 union 的結構中。 是否可以這樣做,還是必須在特定類型上識別它? 我已經嘗試在將結構傳遞給映射之前對其進行初始化,甚至將二進制數據復制到聯合,但每次獲取 typeid(T) 時都會出錯。 而且我還嘗試在地圖中使用元組而不是結構體。 這是導致錯誤的行
line 80 = mProperties[name] = std::make_tuple(typeid(T), d);
錯誤
Severity Code Description Project File Line Suppression State
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::tuple<type_info,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>' (or there is no acceptable conversion) OgreEngine D:\SchoolAndProjects\Programs\ETGG 3802_01 Realtime Interactive Prog 2\Lab 1\OgreEngine\OgreEngine\include\component.h 80
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::tuple<type_info,int>' (or there is no acceptable conversion) OgreEngine D:\SchoolAndProjects\Programs\ETGG 3802_01 Realtime Interactive Prog 2\Lab 1\OgreEngine\OgreEngine\include\component.h 80
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::tuple<type_info,float>' (or there is no acceptable conversion) OgreEngine D:\SchoolAndProjects\Programs\ETGG 3802_01 Realtime Interactive Prog 2\Lab 1\OgreEngine\OgreEngine\include\component.h 80
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::tuple<type_info,double>' (or there is no acceptable conversion) OgreEngine D:\SchoolAndProjects\Programs\ETGG 3802_01 Realtime Interactive Prog 2\Lab 1\OgreEngine\OgreEngine\include\component.h 80
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'std::tuple<type_info,bool>' (or there is no acceptable conversion) OgreEngine D:\SchoolAndProjects\Programs\ETGG 3802_01 Realtime Interactive Prog 2\Lab 1\OgreEngine\OgreEngine\include\component.h 80
Error C2280 'void *std::pair<const std::string,std::tuple<type_info,OgreEngine::Component::data>>::__delDtor(unsigned int)': attempting to reference a deleted function OgreEngine C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 676
Error C2280 'void *std::pair<const std::string,std::tuple<type_info,OgreEngine::Component::data>>::__delDtor(unsigned int)': attempting to reference a deleted function OgreEngine C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 676
Error C2280 'void *std::pair<const std::string,std::tuple<type_info,OgreEngine::Component::data>>::__delDtor(unsigned int)': attempting to reference a deleted function OgreEngine C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 676
Error C2280 'void *std::pair<const std::string,std::tuple<type_info,OgreEngine::Component::data>>::__delDtor(unsigned int)': attempting to reference a deleted function OgreEngine C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 676
Error C2280 'void *std::pair<const std::string,std::tuple<type_info,OgreEngine::Component::data>>::__delDtor(unsigned int)': attempting to reference a deleted function OgreEngine C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 676
代碼:
private:
union data
{
std::string sVal;
bool bVal;
int iVal;
float fVal;
double dVal;
};
struct propertyData {
type_info dataType;
data val;
};
protected:
GameObject* Parent;
std::map<std::string, std::tuple<type_info, data>> mProperties;
public:
/// Append the property of type T to the property map container
template<typename T>
void add_xml_property(std::string name, T d)
{
if (mProperties.find(name) == mProperties.end())
{
//struct propertyData pData = {typeid(T), d};
//memcpy_s((void*)&pData.val, sizeof(d), (void*)&d, sizeof(d));
mProperties[name] = std::make_tuple(typeid(T), d);
}
};
std::type_info
的復制構造函數被刪除,因此您無法將這些對象復制到您的地圖中。 但是, typeid()
...
...指的是一個具有靜態存儲持續時間的對象,多態類型
const std::type_info
或從它派生的某種類型。
...因此您可以在地圖中存儲const std::type_info
指針:
#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
#include <utility>
#include <variant>
using data = std::variant<std::string, bool, int, float, double>;
std::ostream& operator<<(std::ostream& os, const data& v) {
switch(v.index()) {
case 0: os << std::get<0>(v); break;
case 1: os << std::get<1>(v); break;
case 2: os << std::get<2>(v); break;
case 3: os << std::get<3>(v); break;
case 4: os << std::get<4>(v); break;
}
return os;
}
std::map<std::string, std::pair<const std::type_info*, data>> mProps;
template<typename T>
void add_xml_property(const std::string& name, T d) {
if(mProps.find(name) == mProps.end()) mProps[name] = {&typeid(T), d};
}
int main() {
add_xml_property("foo", std::string("hello"));
add_xml_property("bar", 123);
add_xml_property("baz", 3.14159);
for(auto& [k, v] : mProps)
std::cout << k << ": " << v.first->name() << '\n' << v.second << "\n\n";
}
可能的輸出:
bar: i
123
baz: d
3.14159
foo: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
hello
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.