stl

Errors while using templates templates parameters

让人想犯罪 __ 提交于 2021-01-05 06:39:17
问题 I am studying CPP templates myself and got stuck while trying templates of templates parameters for a class. I am getting errors when I am trying to instantiate the class member. #pragma once #include "stdafx.h" # include <list> template<class type, template<type> class T> class stack { private: int count; int size; T<type> st; public: stack(size_t size):size(100), count(-1){ } void push(type elem); type pop(); }; template<class type, template<type> class T> void stack<type, T>::push(type

Compiler-enforced semantic types

∥☆過路亽.° 提交于 2020-12-31 06:29:48
问题 Say I have a class representing automata, whose states are numbered ( using state_t = unsigned ) and whose transitons are also numbered ( using transition_t = unsigned ). Of course at some point I end up messing some calls because transition_t and state_t are the same type, so the compiler does not enforce the (semantic) type safety. That's easy to workaround by using a small class templated by a tag ( struct transition_tag {}; struct state_tag {}; ), so now transition_t and state_t are

Why isn't std::find() using my operator==?

為{幸葍}努か 提交于 2020-12-29 02:39:46
问题 In the following snippet of code, I've overloaded the operator== to compare my pair type with string. But for some reason, the compiler isn't finding my operator as a match for the find function. Why not? Edit: Thanks for all the suggestions for alternatives, but I'd still like to understand why . The code looks like it should work; I'd like to know why it doesn't. #include <vector> #include <utility> #include <string> #include <algorithm> typedef std::pair<std::string, int> RegPair; typedef

Why isn't std::find() using my operator==?

Deadly 提交于 2020-12-29 02:39:31
问题 In the following snippet of code, I've overloaded the operator== to compare my pair type with string. But for some reason, the compiler isn't finding my operator as a match for the find function. Why not? Edit: Thanks for all the suggestions for alternatives, but I'd still like to understand why . The code looks like it should work; I'd like to know why it doesn't. #include <vector> #include <utility> #include <string> #include <algorithm> typedef std::pair<std::string, int> RegPair; typedef

Why isn't std::find() using my operator==?

北城以北 提交于 2020-12-29 02:38:59
问题 In the following snippet of code, I've overloaded the operator== to compare my pair type with string. But for some reason, the compiler isn't finding my operator as a match for the find function. Why not? Edit: Thanks for all the suggestions for alternatives, but I'd still like to understand why . The code looks like it should work; I'd like to know why it doesn't. #include <vector> #include <utility> #include <string> #include <algorithm> typedef std::pair<std::string, int> RegPair; typedef

Why isn't std::find() using my operator==?

不羁岁月 提交于 2020-12-29 02:38:06
问题 In the following snippet of code, I've overloaded the operator== to compare my pair type with string. But for some reason, the compiler isn't finding my operator as a match for the find function. Why not? Edit: Thanks for all the suggestions for alternatives, but I'd still like to understand why . The code looks like it should work; I'd like to know why it doesn't. #include <vector> #include <utility> #include <string> #include <algorithm> typedef std::pair<std::string, int> RegPair; typedef

STL - what is the problem of the following code?

可紊 提交于 2020-12-26 07:02:26
问题 #include "stdafx.h" #include <string> #include <map> using namespace std; class NiftyEmailProgram { private: typedef map<string, string> NicknameMap; NicknameMap nicknames; public: void ShowEmailAddress(const string& nickname) const { NicknameMap::const_iterator i = nicknames.find(nickname); if ( i != nicknames.end() ) { } } }; int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } When I compile the above code in VC6.0, I have seen tons of warnings. If I use Warning Level 4

Odd values printed when dereferencing the end iterator of a vector

拥有回忆 提交于 2020-12-25 04:34:26
问题 I have a vector storing {1,2,3,4,5}. I tried to print *(vec.end()) and got back the result 6. I don't know how to explain this. Similarly, calling vec.find(500) gave the result 6. Why am I getting this number? #include<iostream> #include<iterator> #include<set> #include<map> int main() { int a[] = {1,2,3,4,5}; std::set<int> set1(a,a+sizeof(a)/sizeof(int)); for (std::set<int>::iterator itr=set1.begin();itr!=set1.end();++itr){ std::cout << *itr << std::endl; } //std::pair<std::set<int>:

How will C++20 constexpr containers work?

混江龙づ霸主 提交于 2020-12-25 04:10:28
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated

How will C++20 constexpr containers work?

僤鯓⒐⒋嵵緔 提交于 2020-12-25 04:10:12
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated