在编写一个供个人使用的小模板元编程库时,我遇到了一个有趣的问题.
由于我正在为一些元函数重用一些部分特化,我决定将它们放在一个公共模板类下,并使用标签和嵌套的部分特化来提供行为上的差异.
问题是我得到了荒谬(对我而言)的结果.这是一个最小的例子,展示了我想要做的事情:
#include <iostream>
#include <cxxabi.h>
#include <typeinfo>
template <typename T>
const char * type_name()
{
return abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
}
template <typename... Args>
struct vargs {};
namespace details
{
template <typename K>
struct outer
{
template <typename Arg>
struct inner
{
using result = Arg;
};
};
}
struct tag {};
namespace details
{
template <>
template <typename Arg, typename... Args>
struct outer<tag>::inner<vargs<Arg, Args...>>
{
using result = typename outer<tag>::inner<Arg>::result;
};
}
template <typename T>
using test_t …
Run Code Online (Sandbox Code Playgroud) c++ templates language-lawyer template-meta-programming c++11
有没有办法制作一个类型的完整副本,以便可以在模板推导上下文中区分它们?举个例子:
#include <iostream>
template <typename T>
struct test
{
static int c()
{
static int t = 0;
return t++;
}
};
typedef int handle;
int main()
{
std::cout << test<int>::c() << std::endl;
std::cout << test<handle>::c() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于typedef只为类型创建别名,因此打印0,1而不是所需的0,0.是否有解决方法?
我有关于函数模板参数类型推导过程的问题.
举个例子:
#include <vector>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>
int main()
{
std::ifstream file("path/to/file");
std::vector<int> vec(std::istream_iterator<int>{file},{}); // <- This part
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,第二个参数推断为std::istream_iterator
默认构造函数的类型.
适当的std::vector
构造函数声明为:
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
Run Code Online (Sandbox Code Playgroud)
由于推导出第一个参数类型,因为std::istream_iterator<int>
第二个参数也被推导出来std::istream_iterator<int>
,因此可以应用统一初始化语义.我不知道的是类型推导发生的顺序.我真的很感激这方面的一些信息.
提前致谢!
实际上,有很多方法可以将文件读入字符串.两个常见的是使用ifstream :: read直接读取字符串并使用steambuf_iterators和std :: copy_n:
使用ifstream :: read:
std::ifstream in {"./filename.txt"};
std::string contents;
in.seekg(0, in.end);
contents.resize(in.tellg());
in.seekg(0, in.beg);
in.read(&contents[0], contents.size());
Run Code Online (Sandbox Code Playgroud)
使用std :: copy_n:
std::ifstream in {"./filename.txt"};
std::string contents;
in.seekg(0, in.end);
contents.resize(in.tellg());
in.seekg(0, in.beg);
std::copy_n(std::streambuf_iterator<char>(in),
contents.size(),
contents.begin();
Run Code Online (Sandbox Code Playgroud)
许多基准测试显示第一种方法比第二种方法快得多(在我的机器中使用g ++ - 4.9它使用-O2和-O3标志的速度大约快10倍)我想知道造成这种差异的原因可能是什么.性能.
我正在尝试在Visual Studio 2010中编译下一个代码:
A {
public:
void f(int i) {cout << i;}
};
class B: public A {
public:
void f(string s) {cout << s;}
};
void main() {
A a;
B b;
a.f(1);
b.f("zazaza");
b.f(1); //Compilation fail
}
Run Code Online (Sandbox Code Playgroud)
但编译失败了.我无法理解为什么我不能从父类调用f(int).我该怎么做才能解决这个问题?
我花了一个小时试图找出原因
char buffer[101];
scanf("%100[^\n]", buffer);
Run Code Online (Sandbox Code Playgroud)
按预期工作,读取字符串直到遇到换行符,同时
char buffer[101];
scanf("%100[^\n]\n", buffer);
Run Code Online (Sandbox Code Playgroud)
按Enter键后不返回.
在按Enter后立即用CtrlD(在linux中)显式刷新输入缓冲区似乎解决了问题,强制scanf返回.我在这里错过了什么吗?
我知道我们不能通过引用返回一个局部变量,因为它会超出范围.虽然我在返回传递的引用时有点困惑.例如,以下示例是合法的还是会导致未定义的行为?
classobject &function(classobject &obj) {
return obj;
}
Run Code Online (Sandbox Code Playgroud) 我知道这可能是一个愚蠢的问题,但我不确定如何从终端获得用户输入实际上是有效的.
我从概念上理解输入/输出,我使用它们没有问题,但是当涉及到如何在基本级别实际实现它们时,我迷失了方向.
据我所知,所有流对象都使用一种缓冲区.如果你提取所有到达eof的字符.这部分我可能错了,我想了解更多.例如,当我们使用std :: cin的提取器运算符时,它会等待输入.它如何区分等待输入和达到eof(没有别的东西可读)?
自从我上次查看临时生命周期规则以来已经有一段时间了,我不记得成员右值引用如何影响生命周期。
例如,采用以下两段代码:
int main()
{
std::get<0>(std::forward_as_tuple(
[](int v){ std::cout << v << std::endl; }
))(6);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
,
int main()
{
auto t = std::forward_as_tuple(
[](int v){ std::cout << v << std::endl; }
);
std::get<0>(t)(6);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果成员右值引用不影响生命周期规则,我希望第一个示例表现良好,而第二个示例未定义(因为包含 lambda 对象的完整表达式以第一个分号结尾)。
C++11、C++14 和 C++17 如何处理给定的示例?三者之间有区别吗?
我正在为我的sfml游戏制作一个Entity类,但我无法理解为什么下面的代码无法编译.我收到一个undefined reference to vtable in Entity
错误.
这是头文件:
#ifndef ENTITY_H
#define ENTITY_H
#include <string>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Time.hpp>
class Entity
{
public:
Entity(std::string id, sf::Vector2f position, float rotation );
virtual ~Entity() = 0;
virtual sf::FloatRect getBoundingRect() const;
virtual float getRadius() const;
void UpdateVelocity(sf::Time);
void UpdatePosition(sf::Time);
void Update(sf::Time);
float getRotation() const;
sf::Vector2f getPosition() const;
sf::Vector2f getVelocity() const;
sf::Vector2f getAcceleration() const;
private:
std::string mId;
sf::Vector2f mPosition;
float mRotation;
sf::Vector2f mVelocity;
sf::Vector2f mAcceleration;
};
#endif // ENTITY_H
Run Code Online (Sandbox Code Playgroud)
这是cpp文件:
#include "../include/Entity.h" …
Run Code Online (Sandbox Code Playgroud) 所以我尝试使用自定义文字运算符来实现二进制文字.但是,似乎我在模板专业化方面做错了.static_assert是在不应该:
template <char... bits>
struct bin_imp
{
static constexpr unsigned long long to_ull()
{
static_assert(false,"not binary value");
return 0;
}
};
template <char... bits>
struct bin_imp<'0', bits...>
{
static constexpr unsigned long long to_ull()
{
return bin_imp<bits...>::to_ull();
}
};
template <char... bits>
struct bin_imp<'1', bits...>
{
static constexpr unsigned long long to_ull()
{
return (1ULL << sizeof...(bits)) | bin_imp<bits...>::to_ull();
}
};
template <>
struct bin_imp<>
{
static constexpr unsigned long long to_ull()
{
return 0;
}
};
template <char... bits> …
Run Code Online (Sandbox Code Playgroud) c++ templates static-assert template-specialization variadic-templates
我有一个列表,其中包含其他列表,我想检索具有最少元素的列表.一个明显的解决方案是
list_of_list = ...
least = list_of_lists[0]
for list in list_of_lists[1:]
if len(list) < len(least):
least = list
return least
Run Code Online (Sandbox Code Playgroud)
有没有比较惯用的方法呢?