Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing more demo code to show in C++ Insights #5

Closed
andreasfertig opened this issue May 16, 2018 · 4 comments
Closed

Providing more demo code to show in C++ Insights #5

andreasfertig opened this issue May 16, 2018 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@andreasfertig
Copy link
Owner

One request I got multiple times is to have more example transformations available. In a drop-down list or something. I opened this issue and asking for submissions. Which code-snippets should be on such a list?

@weliveindetail
Copy link

In addition to the existing Range-based For Loop example, one for C++17 Structured Bindings may be a good candidate, e.g:

#include <cstdio>
#include <tuple>

auto mypair() {
  return std::make_tuple("hello", 1);
}

int main() {
  auto [c, n] = mypair();
  printf("c=%s, n=%d\n", c, n);
}

This emits:

int main() {
  auto __mypair9 = mypair();
  std::tuple_element<0, std::tuple<const char *, int> >::type&& c = get<0ul>(__mypair9);
  std::tuple_element<1, std::tuple<const char *, int> >::type&& n = get<1ul>(__mypair9);
  
  printf("c=%s, n=%d\n", c, n);
}

@weliveindetail
Copy link

#include <cstdio>
#include <string>
#include <tuple>
#include <vector>

template <typename Tuple_t>
void fillTuples(std::vector<Tuple_t> tuples) {
  tuples.emplace_back("hello", 1);
}

int main() {
  std::vector<std::tuple<std::string, int>> tuples;
  fillTuples(tuples);
    
  for (auto [s, n] : tuples) {
    printf("c=%s, n=%d\n", s.c_str(), n);
  }
}

@andreasfertig andreasfertig added enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels May 18, 2018
@Amomum
Copy link

Amomum commented Oct 23, 2018

What about recursive templates?

#include <cstdio>
#include <vector>

template<int n>
struct A
{
  static const auto value = A<n-1>::value+n;
};

template<>
struct A<1>
{
	static const auto value = 1;
};


int main()
{
      printf("c=%c\n", A<5>::value);
}

@andreasfertig
Copy link
Owner Author

Hello,

there is now an examples page. Feel free to open pull request for more examples.

Andreas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants