仅当选择了文本文件存档时,以下示例才会编译.随着xml文件的选择,出现了大量的错误消息行.
我用g ++ 4.7.2编译.
该示例包含以下两行:boost :: archive :: xml_oarchive ar(ofs); boost :: archive :: text_oarchive ar(ofs);
xml无法正常工作
(我这里只写了相同的描述很多次,因为我不能在这里发布少于100行描述...... :-()
其中一个错误就是
/usr/include/boost/archive/basic_xml_iarchive.hpp:70:9: error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<Base>::************)'
Run Code Online (Sandbox Code Playgroud)
任何提示?
#include <iostream>
#include <assert.h>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_woarchive.hpp>
#include <boost/archive/xml_wiarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
using namespace std;
class Base
{
public:
int genericData;
Base(int _genericData) : genericData(_genericData) {}
Base() {}
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& ar, const unsigned version)
{
ar …Run Code Online (Sandbox Code Playgroud) 我玩弄模板专业化和 SFINAE。
至于下面的例子,事情似乎很简单:
template <class T>
void Do(T t, typename std::enable_if<std::is_integral<T>::value >::type* = 0)
{
cout << "is integer" << endl;
}
template <class T>
void Do(T t, typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
{
cout << "is float" << endl;
}
Run Code Online (Sandbox Code Playgroud)
不,我尝试过 std::is_array,但从未使用过 std::is_array 的专业化。
所以我尝试了为什么 is_array 从不匹配:
template <int num>
void Do( int a[num])
{
cout << "int array size " << num << endl;
}
void Do( int* x)
{
cout << "int*" << endl;
}
...
int …Run Code Online (Sandbox Code Playgroud) 结构化绑定只能与某种"struct"一起用作返回值吗?
回馈任何类/结构,例如元组在这里工作正常:
auto f()
{
return std::make_tuple(1,2.2);
}
Run Code Online (Sandbox Code Playgroud)
是否有能够实现以下目标的东西:
auto f() -> [int,double]
{
return { 1, 2.2 }; // maybe with or without braces arround
}
Run Code Online (Sandbox Code Playgroud) 我有重载方法:
template<typename AnyType>
void AnyFunc(AnyType &t);
template<typename AnyType>
void AnyFunc(AnyType &&t);
Run Code Online (Sandbox Code Playgroud)
我有一个调用程序,它持有一个指针,并希望使用其中一个函数:
MyType* ptr=new MyType();
AnyFunc(*ptr);
Run Code Online (Sandbox Code Playgroud)
最后一行遇到编译错误:AnyFunc的模糊重载
如何选择我需要的功能?简单的例子是使用:
AnyFunc(std::move(*ptr));
Run Code Online (Sandbox Code Playgroud)
选择void AnyFunc(AnyType &&t);但这不是我想要的.我需要这个
void AnyFunc(AnyType &t);方法.
我不知道使用SFINAE进行方法重载必须考虑哪些规则.我在问题中多次运行,因为据我所知,接缝更多涉及规则.所以我希望有一套规则可以简单解释,以帮助解决一般问题,而不是一次又一次地提问.
我的起点在这里: 如果需要参数包,请使用SFINAE专门课程
代码1
class AA { public: using TRAIT = int; };
class BB { public: using TRAIT = float; };
template < typename T>
class Y
{
public:
template <typename U = T, typename V= typename std::enable_if< std::is_same< int, typename U::TRAIT>::value, int >::type>
Y( ) { std::cout << "First" << std::endl; }
template <typename U = T, typename V= typename std::enable_if< !std::is_same< int, typename U::TRAIT>::value, float >::type>
Y( ) { std::cout << "Second" << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
错误:'模板模板Y …
如果我想使用一些方便的东西,比如make_array我没有机会先声明我的数组,然后再像“早期”那样进行定义,因为我的 var 的类型在定义之前不可用。
所以我找到了这个答案:
对 static constexpr char[] 的未定义引用
在下面的示例中,我编写了这个解决方案,它可以用 gcc 很好地编译,我不确定这是否是真正有效的 C++ 代码,因为它或多或少是一个带有定义的声明,后来是一个没有任何内容的定义。这是允许的吗?(编译良好并不能保证代码是有效的 C++ )
#include <experimental/array>
#include <iostream>
class Foo
{
private:
static decltype(auto) constexpr Bar =
std::experimental::make_array(
std::experimental::make_array( 1,2,3 ),
std::experimental::make_array( 4,5,6 )
);
public:
using ARR_TYPE = decltype( Bar );
static auto& GetArr( int idx )
{
// range check ...
return Bar[idx];
}
};
constexpr Foo::ARR_TYPE Foo::Bar;
int main()
{
for ( auto el: Foo::GetArr(0))
{
std::cout << el << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 对于c ++ 20,建议为通用lambdas p0428r2.pdf添加以下语法
auto f = []<typename T>( T t ) {};
Run Code Online (Sandbox Code Playgroud)
但是gcc 8中的当前实现不接受以下实例化:
f<std::string>("");
Run Code Online (Sandbox Code Playgroud)
这是gcc中的实现错误还是缺少语言功能?我知道我们谈的是提案,而不是批准的规范.
完整示例(与模板函数语法比较):
template <typename T> void n( T t ) { std::cout << t << std::endl; }
auto f = []<typename T>( T t ) { std::cout << t << std::endl; };
int main()
{
f<std::string>("Hello"); // error!
n<std::string>("World");
}
Run Code Online (Sandbox Code Playgroud)
抱怨以下错误:
main.cpp:25:22:错误:在'>'标记f之前的预期primary-expression("Hello");
我开发了一个返回30个数据的代码:
int data1;
int data2;
int data3;
...
float datan;
...
float datam;
Run Code Online (Sandbox Code Playgroud)
除两个外,所有数据都具有相同的类型.我希望同时获得所有这些数据.我的解决方案是在结构中收集所有这些数据,但它并不优雅.你有其他解决方案吗?
在运行以下程序时,我遇到了段错误.但如果我评论memset,我不会得到seg错误.无法弄清楚原因.如果我需要清除结构内容(例如值),我们可以做什么而不是memset?
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
struct test
{
int value;
map<int, int> m;
};
void print(test *ptr)
{
cout << "map size() : " << ptr->m.size();
for(auto itr = ptr->m.begin(); itr != ptr->m.end(); ++itr)
{
}
}
int main()
{
test obj;
memset(&obj, 0, sizeof(obj));
print(&obj);
}
Run Code Online (Sandbox Code Playgroud)